Skip to content

Instantly share code, notes, and snippets.

@ahmad-moussawi
Created April 4, 2015 06:47
Show Gist options
  • Save ahmad-moussawi/3939f0c296fba554d78b to your computer and use it in GitHub Desktop.
Save ahmad-moussawi/3939f0c296fba554d78b to your computer and use it in GitHub Desktop.
SQL Server Create Login with User
-- create a user myUser with myPassword and assign it to myDb
USE [master]
GO
-- Delete existing login.
IF EXISTS (SELECT * FROM sys.server_principals WHERE name = N'myUser')
DROP LOGIN [myUser]
GO
-- Add new login.
CREATE LOGIN [myUser] WITH PASSWORD=N'myPassword', DEFAULT_DATABASE=[myDb]
GO
USE myDb
GO
IF EXISTS (SELECT * FROM sys.database_principals WHERE name = N'myUser')
DROP USER [myUser]
GO
-- Add new user.
CREATE USER [myUser] FOR LOGIN [myUser] WITH DEFAULT_SCHEMA=[STAGE]
GO
-- OPTIONAL if I want to make it admin
EXEC sp_addrolemember 'db_owner', 'myUser'
GO
EXEC sp_addrolemember 'db_datareader', 'myUser'
EXEC sp_addrolemember 'db_datawriter', 'myUser'
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment