Ways to change the owner of a database object

Source: Internet
Author: User

Sp_changeobjectowner changes the owner of the object in the current database.

Grammar:

sp_changeobjectowner [ @objname = ] 'object' ,
[ @newowner = ] 'owner'参数[@objname =] '

Object ' The name of an existing table, view, or stored procedure in the current database.

Object has a data type of nvarchar (517) and no default value. Object can be qualified with an existing object owner, in the form:

existing_owner.object。[@newowner =] 'owner'

The name of the security account that is about to become the new owner of the object.

Owner's data type is sysname, and there is no default value. Owner must be a valid Microsoft®sql Server™ user or role or Microsoft Windows NT® user or group in the current database. When you specify a Windows NT user or group, specify the name that the Windows NT user or group has known in the database (added with sp_grantdbaccess). The return code value of 0 (successful) or 1 (failed) annotation object owner (or a member of the group or role that owns the object) has special permissions on the object.

The object owner can execute any Transact-SQL statement related to the object (such as INSERT, UPDATE, DELETE, SELECT, or EXECUTE), or you can manage the permissions of the object.

If the security account that owns the object must be dropped, but you want to keep the object at the same time, use Sp_changeobjectowner to change the object owner.

This procedure deletes all existing permissions from the object. After you run sp_changeobjectowner, you need to reapply any permissions that you want to keep. For this reason, it is recommended that you write scripts for existing permissions before running Sp_changeobjectowner. Once you have changed the ownership of an object, you may want to reapply the permissions using the script. You need to modify the object owner in the permissions script before running the script. For more information about writing database scripts, see Writing database Documents and scripts.

You can use Sp_changedbowner to change the owner of the database. Permissions can only be performed if the sysadmin fixed server role and the db_owner fixed database role are members, or are members of both the db_ddladmin fixed database role and the db_securityadmin fixed database role sp_ Changeobjectowner.

Example the following example changes the owner of the authors table to:

CorporateGeorgeW。EXEC sp_changeobjectowner
'authors', 'CorporateGeorgeW'
exec sp_changeobjectowner 'ychw.
proc_Subject512_Function', 'dbo'

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.