Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically set a DB user to be db_owner

How can I assign the db_owner role to a user that I have created?

I am able to create a login and add them to the database. I don't know how to change their permission to db_owner using a SQL query.

I have a feeling I am maybe missing something with my query where I add the user to the database?

Here is the query to add the user to the database

CREATE USER [Driver-SOC-ChrisTest] FOR LOGIN [Driver-SOC-ChrisTest] 
WITH DEFAULT_SCHEMA=[dbo]
like image 977
Chris James Avatar asked Jun 13 '11 14:06

Chris James


People also ask

How do I grant privileges to a user in SQL Server?

Login to SQL Server Management Studio. In Object Explorer on the left pane, expand the Databases folder and select the concerned database and navigate to the by expanding Security and Users folders. Right-click the User to which you want to GRANT or REVOKE the permissions.

Can db_owner create users?

Creating a user requires alter any user permission, or membership of the db_accessadmin or db_owner database roles. A database owner is a member of the db_owner role by definition. Creating a login requires the alter any login privilege.

What is the difference between DBO and db_owner?

dbo is a user and db_owner is a database role. Databases are owned by logins. Whatever login owns the database is aliased as dbo inside the database.

How do I give DBO permissions in SQL Server?

Click the Database Access tab. In the list at the top, in the Permit column, select the check box for the database to which you want to assign the owner role for the CES administrative account. In the Permit in Database Role list, select db_owner. Click OK.


2 Answers

To give the user DBO permissions:

EXEC sp_addrolemember N'db_owner', N'[Driver-SOC-ChrisTest]'

To make the user owner of the database (not advised):

EXEC sp_changedbowner N'[Driver-SOC-ChrisTest]'
like image 122
Andomar Avatar answered Oct 15 '22 17:10

Andomar


I quite often go into the GUI, make the changes I need and then rather than saving by pressing OK, I press the Script button at the top of the dialog and send it to a new window.

enter image description here

This would give you the code the previous poster provided.

like image 21
SPE109 Avatar answered Oct 15 '22 16:10

SPE109