declare tb cursor local for
select 'sp_changeobjectowner
''['+replace(user_name(uid),']',']]')+'].['+replace(name,']',']]')+']'',''dbo'''
from sysobjects
where xtype = 'U' and status>=0 and user_name(uid) <> 'dbo'
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' 用户。适用于需要批量更改数据库对象所有者的场景。

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



