SQL Server two ways to modify the owner of an object,

Source: Internet
Author: User
Tags modify
server| objects


General development, SQL Server's database owner is dbo. But for security, it's sometimes possible to replace it with a different name.

The owner transform is not very convenient. Here are two kinds of references

One:

---****************** Change permissions to add HHRC users *************************
--STEP1 Add third party users, such as "CHN"
--STEP2 run SQL, add object permissions to third party users
--STEP3 Add HHRC User
--STEP4 run SQL, move object permissions to 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

The second kind.
--Manually Modify system tables
--The device can modify the table manually
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.