--执行这个语句,就可以把当前库的所有表的所有者改为dbo
exec sp_msforeachtable 'sp_changeobjectowner ''?'', ''dbo'''
--如果是要用户表/存储过程/视图/触发器/自定义函数一齐改,则用游标(不要理会错误提示)
declare tb cursor local for
select 'sp_changeobjectowner ''['+replace(user_name(uid),']',']]')+'].['
+replace(name,']',']]')+']'',''dbo'''
from sysobjects
where xtype in('U','V','P','TR','FN','IF','TF') and status>=0
open tb
declare @s nvarchar(4000)
fetch tb into @s
while @@fetch_status=0
begin
exec(@s)
fetch tb into @s
end
close tb
deallocate tb
go
本文介绍如何通过执行特定的SQL语句批量修改SQL Server中所有表、存储过程、视图等对象的所有者为dbo。包括使用sp_msforeachtable及游标方式实现。
1419

被折叠的 条评论
为什么被折叠?



