數據庫操作當中,當數據庫對象列表不只有一個普通的元素——objectname時,你將要使用objectowner.objectname來引用對象,但如果需要更改當前數據庫中對象的所有者可以使用系統存儲過程 sp_changeobjectowner (點擊這裡更詳細)
sp_changeobjectowner
更改當前數據庫中對象的所有者。
語法: sp_changeobjectowner [ @objname = ] 'object' , [, @newowner = ] 'owner'. 參數. [@objname =] 'object'.
但往往由於數據庫對象過多,希望批處理更改當前數據庫中對象的所有者,那麼你可以嘗試使用Net Fetch的 nf_ChangeObjectOwner 存儲過程來完成批處理。具體用法如下:
首先使用以下代碼創建存儲過程——
nf_ChangeObjectOwner
將Sql Server對象的當前擁有者更改成目標擁有者
語法:nf_ChangeObjectOwner [, @current_Owner = ] 'owner',[,@target_Owner =] 'owner',[ ,@modify_Type=] type
代碼如下:
if exists (select * from sysobjects where id = object_id(N'[nf_ChangeObjectOwner]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure nf_ChangeObjectOwner
GO
Create PROCEDURE nf_ChangeObjectOwner
@current_Owner nvarchar(255),
@target_Owner nvarchar(255),
@modify_Type int
/***********************************************************************************************
nf_ChangeObjectOwner
實現功能: 將Sql Server對象的當前擁有者更改成目標擁有者
**** 使用本代碼前請備份數據庫!
**** 所帶來的安全問題,與俺家的驢子無關!
**** 歡迎斧頭劈我,劈死偶有獎!
調用方法: Exec nf_ChangeObjectOwner @current_Owner,@target_Owner,@modify_Type
輸入參數: @current_Owner nvarchar(255) --對象的當前擁有者
@target_Owner nvarchar(255) --對象的目標擁有者
@modify_Type int --0為默認,更改表的擁有者;1為視圖和存儲過程
輸出參數: RETURN值 = -1 --操作對象為0,操作對象不存在
= -2 --操作失敗,可能對象被鎖定
= 0(默認值) --操作成功,打印更改對象數目@object_Num
@object_Num --SQL 打印值,返回更改成功的對象數目
@Write by Net Fetch. @At 2005/09/12
@Email: cnNetFetch*Gmail.Com blog.ad0.cn
************************************************************************************************/
AS
DECLARE @str_Tbl_Name nvarchar(255),@object_Num int,@current_Owner_uid smallint
Set @object_Num = 0
DECLARE @return_status int
Set @return_status = -1
Set @current_Owner_uid = (Select uid From sysusers Where [Name] = @current_Owner)
If Not (Len(@current_Owner_uid)>0)
RETURN -1
If (@modify_Type = 1)
DECLARE ChangeObjectOwner_Cursor CURSOR FOR Select [Name] From sysobjects Where (type='U' or type='V' or type='P') and userstat=0 and [Name]<>'nf_ChangeObjectOwner' and status>-1 and uid = @current_Owner_uid
Else
DECLARE ChangeObjectOwner_Cursor CURSOR FOR Select [Name] From sysobjects Where (type='U' or type='V' or type='P') and userstat<>0 and [Name]<>'dtproperties' and uid = @current_Owner_uid
OPEN ChangeObjectOwner_Cursor
BEGIN TRANSACTION Change_ObjectOwner
FETCH NEXT FROM ChangeObjectOwner_Cursor INTO @str_Tbl_Name
WHILE (@@FETCH_STATUS = 0)
BEGIN
Set @str_Tbl_Name = @current_Owner + '.' + @str_Tbl_Name
Print @str_Tbl_Name
EXEC @return_status = sp_changeobjectowner @str_Tbl_Name, @target_Owner
IF (@return_status <> 0)
BEGIN
ROLLBACK TRANSACTION Change_ObjectOwner
RETURN -2
END
Set @object_Num = @object_Num + 1
FETCH NEXT FROM ChangeObjectOwner_Cursor INTO @str_Tbl_Name
END
Print @object_Num
COMMIT TRANSACTION Change_ObjectOwner
CLOSE ChangeObjectOwner_Cursor
DEALLOCATE ChangeObjectOwner_Cursor
Go
Usage(使用方法): Exec nf_ChangeObjectOwner 'dbo','你的用戶名',0
——將所有用戶表的擁有者更改成'你的用戶名'
Usage(使用方法): Exec nf_ChangeObjectOwner 'dbo','你的用戶名',1
——將除了用戶表之外數據庫對象(視圖、存儲過程)的擁有者更改成'你的用戶名'