1.修改单个数据表的所有者:
exec sp_changeobjectowner '要改的表名','dbo'
2.批量修改所有数据表的所有者:
EXEC sp_MSforeachtable 'exec sp_changeobjectowner ''?'',''dbo'' '
3.批量修改全部存储过程所有者的存储过程:
CREATE PROCEDURE ChangeProcOwner
@OldOwner
as NVARCHAR(128),--参数原所有者
@NewOwner
as NVARCHAR(128)--参数新所有者
AS
DECLARE @Name as NVARCHAR(128)
DECLARE @Owner
as NVARCHAR(128)
DECLARE @OwnerName
as NVARCHAR(128)
DECLARE curObject CURSOR FOR
select 'Name'
= name,
'Owner'
= user_name(uid)
from sysobjects
where user_name(uid)=@OldOwner and xtype='p'
order by name
OPEN curObject
FETCH NEXT FROM curObject INTO @Name, @Owner
WHILE(@@FETCH_STATUS=0)
BEGIN
if @Owner=@OldOwner
begin
set @OwnerName = @OldOwner + '.' + rtrim(@Name)
exec sp_changeobjectowner @OwnerName, @NewOwner
end
FETCH NEXT FROM curObject INTO @Name, @Owner
END
close curObject
deallocate curObject
GO
执行 exec ChangeProcOwner 'xx','dbo'
或者
exec ChangeProcOwner '?','dbo'
引用
修改所有用户表/存储过程/视图/触发器/自定义函数的所有者为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