-- 批量修改所有表中字段名為 aaa 的字段類型
-- 要保證字段是可以修改的
declare @sql varchar(1000)
declare cur_sql cursor for
select 'alter table ' + a.name + ' alter column ' + b.name + ' varchar(20) '
from sys.objects a,sys.columns b
where a.object_id = b.object_id and b.name = 'aaa' and a.type = 'U'
open cur_sql
fetch cur_sql into @sql
while @@fetch_status = 0
begin
-- 每次修改
execute ( @sql )
fetch cur_sql into @sql
end
close cur_sql
deallocate cur_sql
-- 還有一種方案是把所有的語句 取 出來,然後再運行
-- 如下,把結果集裡的語句copy到查詢分析器裡
select 'alter table ' + a.name + ' alter column ' + b.name + ' varchar(20) '
from sys.objects a,sys.columns b
where a.object_id = b.object_id and b.name = 'aaa' and a.type = 'U'