怎么样写删除表的相同字段的存储过程

beggy 2003-10-12 06:33:52
在一个数据库中有很多表,有很大一部分表都有bbs 这个字段,要删除这个字段,该怎么写存储过程?

我是这样写的:
CREATE PROCEDURE del_col
AS
declare @tblName varchar(100);
begin

declare a_cursor cursor
for
select [name] from sysobjects where xtype='u' and status>0
open a_cursor
fetch next from a_cursor into @tblName
Exec ('alter table '+@tblName+' DROP COLUMN bbs ')
while @@Fetch_status=0
begin
fetch next from a_cursor into @tblName
Exec ('alter table '+@tblName+' DROP COLUMN bbs ')
end

close a_cursor
deallocate a_cursor

end
GO
...全文
78 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
beggy 2003-10-12
  • 打赏
  • 举报
回复
谢谢两位!大力哥的大名早有所闻,果然厉害!
pengdali 2003-10-12
  • 打赏
  • 举报
回复
CREATE PROCEDURE del_col
AS
declare @a varchar(255)

begin

declare a_cursor cursor for select d.name from syscolumns a,sysobjects d where a.id=d.id and d.xtype='U' and a.name='bbs'
open a_cursor
fetch next from a_cursor into @a
while @@Fetch_status=0
begin
Exec ('alter table '+@a+' DROP COLUMN bbs ')
fetch next from a_cursor into @a
end

close a_cursor
deallocate a_cursor

end
GO
RockEx 2003-10-12
  • 打赏
  • 举报
回复

...
declare a_cursor cursor
for
select b.name from syscolumns a join sysobjects b on a.id=b.id where a.name='bbs' and b.xtype='U" and b.status>0
open a_cursor
...
beggy 2003-10-12
  • 打赏
  • 举报
回复
对于那些没有bbs 字段的表就报错!该加个什么条件呢?

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧