【C#】如何更改SQLServer數(shù)據(jù)庫所有者
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
下面的操作存在風(fēng)險(xiǎn),建議做好數(shù)據(jù)庫備份。
1、單個(gè)修改
exec sp_changeobjectowner '[所有者].[對象名]', 'dbo'
例子:
exec sp_changeobjectowner '[test].[dnt_users]', 'dbo'
執(zhí)行后,數(shù)據(jù)庫中dnt_users表的所有者從test變?yōu)?font color="#ff0000">dbo。
2、批量修改
2.1 先在查詢分析器中執(zhí)行下面的sql語句,建立名為changename存儲(chǔ)過程。
CREATE PROCEDURE dbo.changename
@OldOwner as NVARCHAR(128),--參數(shù)原所有者
@NewOwner as NVARCHAR(128)--參數(shù)新所有者
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
2.2 執(zhí)行存儲(chǔ)過程changename
exec changename '原所有者名','新所有者名'
例如:
exec changename 'test','dbo'
則當(dāng)前數(shù)據(jù)庫中所有對象所有者從 test 變?yōu)?dbo。
3、相關(guān)教程:
該文章在 2021/6/5 11:45:58 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |