From:
HTTP: // Www.pconline.com.cn / Pcjob / Other / Data / Others / 0611 / 903723 _ 3 . Html
Create Procedure DBO. user_changeobjectownerbatch ( @ 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
-- Select @ name, @ newowner, @ oldowner
Fetch Next From Curobject Into @ Name , @ Owner
End
Close Curobject
Deallocate Curobject
Go
-- Test
Exec User_changeobjectownerbatch ' BMS ' , ' DBO '
2. Additional database:
Exec Sp_attach_db @ Dbname = '' HW '' , @ Filename1 = '' C: \ hw_data.mdf '' , @ Filename2 = '' C: \ hw_log.ldf ''