Created
April 4, 2015 06:47
-
-
Save ahmad-moussawi/3939f0c296fba554d78b to your computer and use it in GitHub Desktop.
SQL Server Create Login with User
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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