Update mssql.JSON (#1803)

I modified lots of snippets to include the database name so that the user can additionally tab through the database name. I made some quality of life change for the user.  I normalized and standardized snippets to look more like one user created the file rather than each script taking on various flavors from the contributors. I tried to write the comments in the snippets in the same style at the majority of the snippets.  I added highlighting in key areas that were missing it or were not replacing the comments. Additionally there was no snippets for sqlCreateIndex and sqlCreateTempTable which I find standard for SQL users.  Let me know your thoughts! No trying to offend anyone if I changed your code.
This commit is contained in:
JASM2007
2018-07-03 11:42:56 -04:00
committed by Karl Burtram
parent 5b19d2b1fc
commit 2da67567e4

View File

@@ -8,9 +8,9 @@
"GO", "GO",
"-- Create the new database if it does not exist already", "-- Create the new database if it does not exist already",
"IF NOT EXISTS (", "IF NOT EXISTS (",
"\tSELECT name", "\tSELECT [name]",
"\t\tFROM sys.databases", "\t\tFROM sys.databases",
"\t\tWHERE name = N'${1:DatabaseName}'", "\t\tWHERE [name] = N'${1:DatabaseName}'",
")", ")",
"CREATE DATABASE ${1:DatabaseName}", "CREATE DATABASE ${1:DatabaseName}",
"GO" "GO"
@@ -29,9 +29,9 @@
"-- ALTER DATABASE ${1:DatabaseName} SET SINGLE_USER WITH ROLLBACK IMMEDIATE;", "-- ALTER DATABASE ${1:DatabaseName} SET SINGLE_USER WITH ROLLBACK IMMEDIATE;",
"-- Drop the database if it exists", "-- Drop the database if it exists",
"IF EXISTS (", "IF EXISTS (",
" SELECT name", " SELECT [name]",
" FROM sys.databases", " FROM sys.databases",
" WHERE name = N'${1:DatabaseName}'", " WHERE [name] = N'${1:DatabaseName}'",
")", ")",
"DROP DATABASE ${1:DatabaseName}", "DROP DATABASE ${1:DatabaseName}",
"GO" "GO"
@@ -42,38 +42,33 @@
"Create a new Table": { "Create a new Table": {
"prefix": "sqlCreateTable", "prefix": "sqlCreateTable",
"body": [ "body": [
"-- Create a new table called '${1:TableName}' in schema '${2:SchemaName}'", "-- Create a new table called '[${1:TableName}]' in schema '[${2:SchemaName}]' in database '[${3:DatabaseName}]'",
"-- Drop the table if it already exists", "-- Drop the table if it already exists",
"IF OBJECT_ID('${2:SchemaName}.${1:TableName}', 'U') IS NOT NULL", "IF OBJECT_ID('[${3:DatabaseName}].[${2:SchemaName}].[${1:TableName}]', 'U') IS NOT NULL",
"DROP TABLE ${2:SchemaName}.${1:TableName}", "DROP TABLE [${3:DatabaseName}].[${2:SchemaName}].[${1:TableName}]",
"GO", "GO",
"-- Create the table in the specified schema", "-- Create the table in the specified database and schema",
"CREATE TABLE ${2:SchemaName}.${1:TableName}", "CREATE TABLE [${3:DatabaseName}].[${2:SchemaName}].[${1:TableName}]",
"(", "(",
"\t${1:TableName}Id INT NOT NULL PRIMARY KEY, -- primary key column", "\t[${4:ColumnName}]Id INT NOT NULL PRIMARY KEY, -- Primary Key column",
"\t$3Column1 [NVARCHAR](50) NOT NULL,", "\t[${5:ColumnName1}] [NVARCHAR](50) NOT NULL,",
"\t$4Column2 [NVARCHAR](50) NOT NULL", "\t[${6:ColumnName2}] [NVARCHAR](50) NOT NULL",
"\t-- specify more columns here", "\t-- Specify more columns here",
");", ");",
"GO" "GO"
], ],
"description": "Create a new Table" "description": "Create a new Table"
}, },
"Drop a Table": { "Drop a Table": {
"prefix": "sqlDropTable", "prefix": "sqlDropTable",
"body": [ "body": [
"-- Drop the table '${1:TableName}' in schema '${2:SchemaName}'", "-- Drop a table called '${3:TableName}' in schema '${2:SchemaName}' in Database '${1:DatabaseName}'",
"IF EXISTS (", "-- Drop the table if it already exists",
"\tSELECT *", "IF OBJECT_ID('[${1:DatabaseName}].[${2:SchemaName}].[${3:TableName}]', 'U') IS NOT NULL",
"\t\tFROM sys.tables", "DROP TABLE [${1:DatabaseName}].[${2:SchemaName}].[${3:TableName}]",
"\t\tJOIN sys.schemas", "GO"
"\t\t\tON sys.tables.schema_id = sys.schemas.schema_id",
"\tWHERE sys.schemas.name = N'${2:SchemaName}'",
"\t\tAND sys.tables.name = N'${1:TableName}'",
")",
"\tDROP TABLE ${2:SchemaName}.${1:TableName}",
"GO"
], ],
"description": "Drop a Table" "description": "Drop a Table"
}, },
@@ -81,9 +76,9 @@
"Add a new column to a Table": { "Add a new column to a Table": {
"prefix": "sqlAddColumn", "prefix": "sqlAddColumn",
"body": [ "body": [
"-- Add a new column '${1:NewColumnName}' to table '${2:TableName}' in schema '${3:SchemaName}'", "-- Add a new column '[${1:NewColumnName}]' to table '[${2:TableName}]' in schema '[${3:SchemaName}]' in database '[${4:DatabaseName}]'",
"ALTER TABLE ${3:SchemaName}.${2:TableName}", "ALTER TABLE [${4:DatabaseName}].[${3:SchemaName}].[${2:TableName}]",
"\tADD ${1:NewColumnName} /*new_column_name*/ int /*new_column_datatype*/ NULL /*new_column_nullability*/", "\tADD [${1:NewColumnName}] /*new_column_name*/ ${5:int} /*new_column_datatype*/ ${6:NULL} /*new_column_nullability*/",
"GO" "GO"
], ],
"description": "Add a new column to a Table" "description": "Add a new column to a Table"
@@ -92,9 +87,9 @@
"Drop a column from a Table": { "Drop a column from a Table": {
"prefix": "sqlDropColumn", "prefix": "sqlDropColumn",
"body": [ "body": [
"-- Drop '${1:ColumnName}' from table '${2:TableName}' in schema '${3:SchemaName}'", "-- Drop '[${1:ColumnName}]' from table '[${2:TableName}]' in schema '[${3:SchemaName}]' in database '[${4:DatabaseName}]'",
"ALTER TABLE ${3:SchemaName}.${2:TableName}", "ALTER TABLE [${4:DatabaseName}].[${3:SchemaName}].[${2:TableName}]",
"\tDROP COLUMN ${1:ColumnName}", "\tDROP COLUMN [${1:ColumnName}]",
"GO" "GO"
], ],
"description": "Add a new column to a Table" "description": "Add a new column to a Table"
@@ -103,9 +98,9 @@
"Select rows from a Table or a View": { "Select rows from a Table or a View": {
"prefix": "sqlSelect", "prefix": "sqlSelect",
"body": [ "body": [
"-- Select rows from a Table or View '${1:TableOrViewName}' in schema '${2:SchemaName}'", "-- Select rows from a Table or View '[${1:TableOrViewName}]' in schema '[${2:SchemaName}]' in database '[${3:DatabaseName}]'",
"SELECT * FROM ${2:SchemaName}.${1:TableOrViewName}", "SELECT * FROM [${3:DatabaseName}].[${2:SchemaName}].[${1:TableOrViewName}]",
"WHERE $3\t/* add search conditions here */", "WHERE ${4:/* add search conditions here */}",
"GO" "GO"
], ],
"description": "Select rows from a Table or a View" "description": "Select rows from a Table or a View"
@@ -114,19 +109,19 @@
"Insert rows into a Table": { "Insert rows into a Table": {
"prefix": "sqlInsertRows", "prefix": "sqlInsertRows",
"body": [ "body": [
"-- Insert rows into table '${1:TableName}'", "-- Insert rows into table '${1:TableName}' in schema '[${2:SchemaName}]' in database '[${3:DatabaseName}]'",
"INSERT INTO ${1:TableName}", "INSERT INTO [${3:DatabaseName}].[${2:SchemaName}].[${1:TableName}]",
"( -- columns to insert data into", "( -- Columns to insert data into",
" $2[Column1], [Column2], [Column3]", " ${4:[ColumnName1], [ColumnName2], [ColumnName3]}",
")", ")",
"VALUES", "VALUES",
"( -- first row: values for the columns in the list above", "( -- First row: values for the columns in the list above",
" $3Column1_Value, Column2_Value, Column3_Value", " ${5:ColumnValue1, ColumnValue2, ColumnValue3}",
"),", "),",
"( -- second row: values for the columns in the list above", "( -- Second row: values for the columns in the list above",
" $4Column1_Value, Column2_Value, Column3_Value", " ${6:ColumnValue1, ColumnValue2, ColumnValue3}",
")", ")",
"-- add more rows here", "-- Add more rows here",
"GO" "GO"
], ],
"description": "Insert rows into a Table" "description": "Insert rows into a Table"
@@ -135,9 +130,9 @@
"Delete rows from a Table": { "Delete rows from a Table": {
"prefix": "sqlDeleteRows", "prefix": "sqlDeleteRows",
"body": [ "body": [
"-- Delete rows from table '${1:TableName}'", "-- Delete rows from table '[${1:TableName}]' in schema '[${2:SchemaName}]' in database '[${3:DatabaseName}]'",
"DELETE FROM ${1:TableName}", "DELETE FROM [${3:DatabaseName}].[${2:SchemaName}].[${1:TableName}]",
"WHERE $2\t/* add search conditions here */", "WHERE ${4:/* add search conditions here */}",
"GO" "GO"
], ],
"description": "Delete rows from a Table" "description": "Delete rows from a Table"
@@ -146,13 +141,13 @@
"Update rows in a Table": { "Update rows in a Table": {
"prefix": "sqlUpdateRows", "prefix": "sqlUpdateRows",
"body": [ "body": [
"-- Update rows in table '${1:TableName}'", "-- Update rows in table '[${1:TableName}]' in schema '[${2:SchemaName}]' in database '[${3:DatabaseName}]'",
"UPDATE ${1:TableName}", "UPDATE [${3:DatabaseName}].[${2:SchemaName}].[${1:TableName}]",
"SET", "SET",
"\t$2[Colum1] = Colum1_Value,", "\t[${4:ColumnName1}] = ${5:ColumnValue1},",
"\t$3[Colum2] = Colum2_Value", "\t[${6:ColumnName2}] = ${7:ColumnValue2}",
"\t-- add more columns and values here", "\t-- Add more columns and values here",
"WHERE $4\t/* add search conditions here */", "WHERE ${8:/* add search conditions here */}",
"GO" "GO"
], ],
"description": "Update rows in a Table" "description": "Update rows in a Table"
@@ -207,8 +202,8 @@
"prefix": "sqlListTablesAndViews", "prefix": "sqlListTablesAndViews",
"body": [ "body": [
"-- Get a list of tables and views in the current database", "-- Get a list of tables and views in the current database",
"SELECT table_catalog [database], table_schema [schema], table_name name, table_type type", "SELECT table_catalog [database], table_schema [schema], table_name [name], table_type [type]",
"FROM information_schema.tables", "FROM INFORMATION_SCHEMA.TABLES",
"GO" "GO"
], ],
"description": "List tables and vies in the current database" "description": "List tables and vies in the current database"
@@ -218,7 +213,7 @@
"prefix": "sqlListDatabases", "prefix": "sqlListDatabases",
"body": [ "body": [
"-- Get a list of databases", "-- Get a list of databases",
"SELECT name FROM sys.databases", "SELECT [name] FROM sys.databases",
"GO" "GO"
], ],
"description": "List databases" "description": "List databases"
@@ -232,8 +227,8 @@
"\tTableName = tbl.table_schema + '.' + tbl.table_name, ", "\tTableName = tbl.table_schema + '.' + tbl.table_name, ",
"\tColumnName = col.column_name, ", "\tColumnName = col.column_name, ",
"\tColumnDataType = col.data_type", "\tColumnDataType = col.data_type",
"FROM information_schema.tables tbl", "FROM INFORMATION_SCHEMA.TABLES tbl",
"INNER JOIN information_schema.columns col ", "INNER JOIN INFORMATION_SCHEMA.COLUMNS col ",
"\tON col.table_name = tbl.table_name", "\tON col.table_name = tbl.table_name",
"\tAND col.table_schema = tbl.table_schema", "\tAND col.table_schema = tbl.table_schema",
"", "",
@@ -247,20 +242,20 @@
"prefix": "sqlCursor", "prefix": "sqlCursor",
"body": [ "body": [
"-- Declare a cursor for a Table or a View '${1:TableOrViewName}' in schema '${2:SchemaName}'", "-- Declare a cursor for a Table or a View '${1:TableOrViewName}' in schema '${2:SchemaName}'",
"DECLARE @Column1 NVARCHAR(50), @Column2 NVARCHAR(50)", "DECLARE @ColumnName1 NVARCHAR(50), @ColumnName2 NVARCHAR(50)",
"", "",
"DECLARE db_cursor CURSOR FOR", "DECLARE db_cursor CURSOR FOR",
"SELECT Column1, Column2", "SELECT ColumnName1, Column2",
"FROM $2.$1", "FROM $2.$1",
"", "",
"OPEN db_cursor", "OPEN db_cursor",
"FETCH NEXT FROM db_cursor INTO @Column1, @Column2", "FETCH NEXT FROM db_cursor INTO @ColumnName1, @ColumnName2",
"", "",
"WHILE @@FETCH_STATUS = 0", "WHILE @@FETCH_STATUS = 0",
"BEGIN", "BEGIN",
"\t-- add instructions to be executed for every row", "\t-- add instructions to be executed for every row",
"\t$3", "\t$3",
"\tFETCH NEXT FROM db_cursor INTO @Column1, @Column2", "\tFETCH NEXT FROM db_cursor INTO @ColumnName1, @ColumnName2",
"END", "END",
"", "",
"CLOSE db_cursor", "CLOSE db_cursor",
@@ -303,5 +298,34 @@
"GO" "GO"
], ],
"description": "Get Space Used by Tables" "description": "Get Space Used by Tables"
} },
"Create a new Index": {
"prefix": "sqlCreateIndex",
"body": [
"-- Create a nonclustered index with or without a unique constraint",
"-- Or create a clustered index on table '[${1:TableName}]' in schema '[${2:SchemaName}]' in database '[${3:DatabaseName}]'",
"CREATE ${5:/*UNIQUE or CLUSTERED*/} INDEX IX_${4:IndexName} ON [${3:DatabaseName}].[${2:SchemaName}].[${1:TableName}] ([${6:ColumnName1}] DESC /*Change sort order as needed*/",
"GO"
],
"description": "Create a new Index"
},
"Create a new Temporary Table": {
"prefix": "sqlCreateTempTable",
"body": [
"-- Drop a temporary table called '#${1:TableName}'",
"-- Drop the table if it already exists",
"IF OBJECT_ID('tempDB..#${1:TableName}', 'U') IS NOT NULL",
"DROP TABLE #${1:TableName}",
"GO",
"-- Create the temporary table from a physical table called '${4:TableName}' in schema '${3:SchemaName}' in database '${2:DatabaseName}'",
"SELECT *",
"INTO #${1:TableName}",
"FROM [${2:DatabaseName}].[${3:[SchemaName}].[${4:TableName}]",
"WHERE ${5:/* add search conditions here */}"
],
"description": "Create a new Temporary Table"
},
} }