Add user to a database - TSQL and SQL Server

SQLSQL Server

Please contribute by voting. Thanks!
1

This is how you add an existing user to a SQL Server (or Azure SQL) database via TSQL:

Prerequisites: The login My_User already exists in the SQL Server.

Step 1 - Connect to the correct database

USE My_Database
GO

Step 2 - Create the user:

CREATE USER My_User FOR LOGIN My_User; 

Steg 3 - Grant the user access

EXEC sp_addrolemember N'db_owner', N'My_User'
GO

Note: give the user as litte access as possible. You can read more about SQL Server datbase-level roles here.

Article created: Jan 10. Edited Jan 10.

Your comment

You need to sign up / log in to comment this article

Author

Created by Faith Logan [6] Jan 10

Share article

Do you know about

LINQ?

Write an article