Wednesday, August 4, 2010

Changing the Database Owner in a SQL Server Database

In SQL Server 2000, the dbo (Database Owner) is a special user that has all the necessary permissions to perform all activities in the database. This user exists in all SQL Server databases.

Usually, there are SQL Server or Windows logins mapped to the dbo user, thus "inheriting" the permissions of the database's dbo user and being able to perform all the activities in the database that the dbo user can perform.

In some cases, due to various changes a DBA might perform in SQL Server, you may discover that the dbo user is orphaned, meaning that there is not any login mapped to this user.

If you would like to change this, thus mapping a login to the dbo user you can make use of the "sp_changedbowner" system stored procedure.

The syntax as provided in the T-SQL reference library is the following:


sp_changedbowner [ @loginame= ] 'login'
[ , [ @map= ] remap_alias_flag ]

For example, if you want to make the SQL Server Login 'Tom' the owner of the database "TestDB" you can use the following T-SQL script:

USE TestDB
GO

EXEC sp_changedbowner 'Tom'
GO

Additionally, if you want to make a Windows Login, for example 'SampleDomain\TestUser' the owner of the database "TestDB" you can use the following T-SQL script:

USE TestDB
GO

EXEC sp_changedbowner 'SampleDomain\TestUser'
GO

Please also note that only members of the sysadmin fixed server role can execute the stored procedure sp_changedbowner.


In SQL Server 2005 or later, you can make use of the "ALTER AUTHORIZATION" command as sp_changedbowner will be removed in a future version of SQL Server.
[Ads]
Check out my latest eBook on SQL Server:
Tuning SQL Server - Ebook

Easily secure your SQL Server instances! Try out DBA Security Advisor!
DBA Security Advisor for SQL Server by SQLArtBits
DBA Security Advisor was developed by SQLArtBits. The tool was being carefully developed for an entire year and it contains comprehensive security checks and recommendations based on proven SQL Server security best practices. Feel free to download and use the Community Edition which is free and if you find it useful, you can consider upgrading to the Enterprise Edition!

Artemakis Artemiou [MVP]

Author & Editor

Artemakis Artemiou is a Senior SQL Server Architect, Software Developer and Microsoft Data Platform MVP. He is also an author, regular blogger, president of Cyprus.NET User Group and CY Country leader of INETA-EU. He is also the creator of DBA Security Advisor and In-Memory OLTP Simulator. Artemakis is a frequent guest author of worldwide well-respected online journals where he writes articles focusing on many SQL Server topics.

Reference: The SQL Server and .NET Hub (http://www.sqlnethub.com)

0 comments: