Created
September 22, 2023 17:14
-
-
Save jknopp/85a4a8670804c13cc32c1950cac8448a to your computer and use it in GitHub Desktop.
Create SQL Contained 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
DECLARE @UName varchar(50) | |
DECLARE @PW varchar(50) | |
DECLARE @SQL varchar(400) | |
SET @UName='connection-string-user' | |
SET @PW='password' | |
SET @SQL='DROP USER IF EXISTS ['+@UName+'];'+ | |
' CREATE USER ['+@UName+'] WITH PASSWORD = '''+@PW+''', DEFAULT_SCHEMA = dbo;'+ | |
' GRANT VIEW DEFINITION TO ['+@UName+'];'+ | |
' EXEC sp_addrolemember ''db_owner'', '''+@UName+''';'+ | |
' ALTER ROLE db_owner ADD MEMBER ['+@UName+'];' | |
--SELECT @SQL | |
EXEC (@SQL); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment