sql server数据库中为每个用户表添加一个新的字段 如何实现

如题所述

使用游标+动态SQL,根据sys.objects来处理追问

这个不会整啊 举个例子吧 我忘数据库名字是aaa的数据库中的每个表添加一个字段bbb int类型如何写啊

追答

use aaa
go
declare @obj_name varchar(128)
declare @sql varchar(2000)
declare cur_obj cursor for
select name from sys.objects where type = 'U'
open cur_obj
fetch cur_obj into @obj_name
while @@fetch_status = 0
begin
exec ( ' alter table ' + @obj_name + ' add bbb int ')
fetch cur_obj into @obj_name
end
close cur_obj
deallocate cur_obj

温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-08-22
为什么会有这样的需求?
相似回答
大家正在搜