February 14, 2014 by Nitesh

How to Add User to SQL Server Database using Scripts

While development, we normally use SQL Server Management Studio for various operations. However, when the code goes in production, Management studio is not found and we need to Execute SQL Queries using sqlcmd tool. Very often, we use a separate user account to access the database used by our program. This post will explain you to create a new SQL Server login and user to access the database.

In this post, we will create a SQL Server Login named “NITESH” with password “MyPassword”. This login will have default database set to “MyDB” and will be the owner of the database.

CREATE LOGIN NITESH WITH PASSWORD = 'MyPassword', DEFAULT_DATABASE = [MyDB]
GO
USE MyDB;
CREATE USER NITESH FOR LOGIN NITESH;
EXEC sp_addrolemember 'db_owner', 'NITESH'
GO

Hope this small tip helps you!

#SQL Server#Users

Support us!

If you like this site please help and make click on any of these buttons!

Powered by WordPress Popup