I only tested tables and stored procedures, and other objects were not actually tested,
Back up the database before use ,
I am not responsible for any errorsPai_^ .
1 /*
2 Brief: Change the owner of all objects in the database
3 Author: Wu erping
4 Date: 2005/12/19
5 */
6
7 Declare @ Owner Varchar ( 255 )
8 Set @ Owner = ' DBO '
9
10 Declare @ Objectname Varchar ( 255 )
11 Declare @ Uid Int
12 Declare Object_cursor Cursor For Select [ Name ] , Uid From Sysobjects
13 Where Objectproperty (ID, ' Ismsshipped ' ) = 0
14 And (Xtype like ' % ' ) -- If you want to specify the object type, refer to transact-SQL help-> sysobjects-> xtype
15 Open Object_cursor
16 Fetch Next From Object_cursor Into @ Objectname , @ Uid
17 While ( @ Fetch_status = 0 )
18 Begin
19 Declare @ Uidname Varchar ( 255 )
20 Select @ Uidname = [ Name ] From DBO. sysusers Where UID = @ Uid
21 If @ Uidname <> @ Owner
22 Begin
23 -- Generate a string in the format of 'owner. Object name'
24 Set @ Objectname = @ Uidname + ' . ' + @ Objectname
25 -- Change object owner
26 Exec Sp_changeobjectowner @ Objectname , @ Owner
27 End
28
29 Fetch Next From Object_cursor Into @ Objectname , @ Uid
30 End
31 Close Object_cursor
32 Deallocate Object_cursor
33 Go