Change the owner of all objects of SQL Server 2000

Source: Internet
Author: User
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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.