แก้ไข

แชร์ผ่าน


sp_changedbowner (Transact-SQL)

Applies to: SQL Server

Changes the owner of the current database.

Important

This feature will be removed in a future version of SQL Server. Avoid using this feature in new development work, and plan to modify applications that currently use this feature. Use ALTER AUTHORIZATION instead.

Transact-SQL syntax conventions

Syntax

sp_changedbowner
    [ @loginame = ] N'loginame'
    [ , [ @map = ] 'map' ]
[ ; ]

Arguments

[ @loginame = ] N'loginame'

The login ID of the new owner of the current database. @loginame is sysname, with no default. @loginame must be an already existing SQL Server login or Windows user. @loginame can't become the owner of the current database if it already has access to the database through an existing user security account within the database. To avoid this scenario, drop the user within the current database first.

[ @map = ] 'map'

This parameter is deprecated and is maintained for backward compatibility of scripts.

Return code values

0 (success) or 1 (failure).

Remarks

After sp_changedbowner is executed, the new owner is known as the dbo user inside the database. The dbo user has implied permissions to perform all activities in the database.

The owner of the master, model, or tempdb system databases can't be changed.

To display a list of the valid @loginame values, execute the sp_helplogins stored procedure.

Executing sp_changedbowner with only the @loginame parameter changes database ownership to @loginame.

You can change the owner of any securable by using the ALTER AUTHORIZATION statement. For more information, see ALTER AUTHORIZATION.

Permissions

Requires TAKE OWNERSHIP permission on the database. If the new owner has a corresponding user in the database, requires IMPERSONATE permission on the login, otherwise requires CONTROL SERVER permission on the server.

Examples

The following example makes the login Albert the owner of the current database.

EXEC sp_changedbowner 'Albert';