--功能說明:成批更改數據庫所有者的對象
--作者:不詳
--用法:exec ChangeObjectOwner 'nmkspro','dbo'
--即可將所有nmkspro所有者的對象改為dbo所有
--運行成功後將提示:"注意: 更改對象名的任一部分都可能破壞腳本和存儲過程。"
CREATE PROCEDURE dbo.ChangeObjectOwner
@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
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 sp_MSforeachtable 'exec sp_changeobjectowner ''?'',''dbo'' '
單個修改:
exec sp_changeobjectowner '要改的表名','dbo'
方法一:直接點擊該表-》設計-》屬性-》更改上面的所有者。該方法針對於表少的情況下。
方法二:利用腳本直接執行,用系統帳號或者超戶登陸到該數據庫,然後執行下面語句:
sp_configure 'allow updates','1'
go
reconfigure with override
go
update sysobjects set uid=1 where uid<>1 --根據條件可以任意修改,在多用戶之間切換。
go
sp_configure 'allow updates','0'
go
reconfigure with override