Code
CREATE PROCEDURE dbo.ChangeObjectOwner
@CurrentOwner 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)=@CurrentOwner --and xtype='p'
order by name
OPEN curObject
FETCH NEXT FROM curObject INTO @Name, @Owner
WHILE(@@FETCH_STATUS=0)
BEGIN
if @Owner=@CurrentOwner
begin
set @OwnerName = @CurrentOwner + '.' + rtrim(@Name)
exec sp_changeobjectowner @OwnerName, @NewOwner
end
FETCH NEXT FROM curObject INTO @Name, @Owner
END
close curObject
deallocate curObject
批量修改:
EXEC sp_MSforeachtable 'exec sp_changeobjectowner ''?'',''dbo'' '
单个修改:
EXEC sp_changeobjectowner '要改的表名','dbo'
如果想修改存储过程Owner:
先把--and xtype='p' 反注释,再创建sp_changeobjectowner 存储过程。
然后执行:EXEC sp_changeobjectowner 'CurrentOwner','dbo'