Skip to content

Instantly share code, notes, and snippets.

@lukencode
Created August 18, 2020 06:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lukencode/f2c0984e8cee48c91ba3764ed60b596d to your computer and use it in GitHub Desktop.
Save lukencode/f2c0984e8cee48c91ba3764ed60b596d to your computer and use it in GitHub Desktop.
Azure Data Studio - User snippets
{
// Place your snippets for sql here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
// "Print to console": {
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }
"Create Database": {
"prefix": "sqlCreateDatabaseAndUser",
"body": [
"-- Create a new database called '${1:DatabaseName}' with user '${2:${1:DatabaseName}-user}' with password '${3:${1:DatabaseName}-password-!@#$@!1}'",
"if db_id('${1:DatabaseName}') is null",
"BEGIN EXEC ('create database [${1:DatabaseName}]') END",
"GO",
"IF NOT EXISTS (SELECT name FROM master.sys.server_principals WHERE name = '${2}')",
"BEGIN EXEC sp_addlogin '${2}', '${3}', 'master'; END",
"GO",
"use [${1:DatabaseName}] IF NOT EXISTS (SELECT * FROM [sys].[database_principals] WHERE [type] = 'S' AND name = N'${2}') BEGIN CREATE USER [${2}] FOR LOGIN [${2}] WITH DEFAULT_SCHEMA=[dbo] END",
"GO",
"EXEC ('use [${1:DatabaseName}] EXEC sp_addrolemember ''db_owner'', ''${2}''')"
],
"description": "Create a new database/user/login"
},
"Restore Database (WITH REPLACE)": {
"prefix": "sqlRestoreWithReplace",
"body": [
"RESTORE FILELISTONLY",
"FROM DISK = '${2:BackupPath}'",
"GO",
"ALTER DATABASE [${1:DatabaseName}]",
"SET SINGLE_USER WITH",
"ROLLBACK IMMEDIATE",
"RESTORE DATABASE [${1:DatabaseName}]",
"FROM DISK = '${2:BackupPath}'",
"WITH REPLACE",
"ALTER DATABASE [${1:DatabaseName}] SET MULTI_USER",
"GO"
],
"description": "Restores database with replace"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment