SQL Server can be used to modify the object owner,

Source: Internet
Author: User

In general development, the database owner of SQL Server is DBO. But for security, it may be changed to another name.

It is not very convenient to change the owner. Two types are listed here for reference.

I:

-- -***************** Change permissions to add hhrc users *************** **********
-- Step 1 add a third-party user, such as "CHN"
-- Step 2 run the SQL statement and add the object permission to a third-party user
-- Step 3 add an hhrc user
-- Step 4 run the SQL statement and move the object permission to the hhrc user
Declare   @ Tblname   Varchar ( 30 )
Declare   @ Tblown   Varchar ( 50 )
Declare Tbl_cur Cursor   For  
Select Name From Sysobjects Where UID = ' 5 '   And Status > = 0   And Xtype In ( ' U ' , ' P ' )
-- Select * From sysusers
Open Tbl_cur
Fetch   Next   From Tbl_cur Into   @ Tblname
While   @ Fetch_status = 0
Begin
Set   @ Tblown = ' Web56433. ' + @ Tblname
Exec Sp_changeobjectowner @ Tblown , ' CHN '
Print   @ Tblname
Fetch   Next   From Tbl_cur Into   @ Tblname
End
Close Tbl_cur
Deallocate Tbl_cur

Second. -- Manually modify system table
-- The device can manually modify the table
Exec Sp_configure ' Allow updates ' , 1
Reconfigure   With Override
-- Change object owner
Update Sysobjects Set UID =   1   Where UID =   5

Update Sysobjects Set UID =   5   Where UID =   1   And Xtype ! =   ' S '

Update Sysobjects Set UID =   1 Where Name =   ' Syssegments '

Update Sysobjects Set UID =   1 Where Name =   ' Sysconstraints '

Exec Sp_configure ' Allow updates ' , 0
Reconfigure   With Override

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.