Snippets: fix sqlCreateTable, remove database refs & use dbo schema (#3094)

- Fixed issues with sqlCreateTable snippet, which meant it couldn't effectively be used to tab through all fields and then hit execute without errors. Specifically fixed bugs where types like NVARCHAR were incorrectly escaped and the Id part of a column was outside the name section, both causing intellisense & execution breaks
- Changed SchemaName to dbo. This helps hit the "80% case" where objects are in the most common schema
- Removed DatabaseName from most snippets. The core issue is this requires you to manually type the exact database name into the snippet which is really hard. We should consider having separate "with Database" snippets or support SQLCMD variables which would let us default to the current database without needing users to manually type them in as alternatives, but having basic snippets just work on current DB is important.
This commit is contained in:
Kevin Cunnane
2018-11-02 14:26:03 -07:00
committed by Karl Burtram
parent b6c9a3bb89
commit 00cd772cbc

View File

@@ -42,18 +42,18 @@
"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}]' in database '[${3:DatabaseName}]'", "-- Create a new table called '[${1:TableName}]' in schema '[${2:dbo}]'",
"-- Drop the table if it already exists", "-- Drop the table if it already exists",
"IF OBJECT_ID('[${3:DatabaseName}].[${2:SchemaName}].[${1:TableName}]', 'U') IS NOT NULL", "IF OBJECT_ID('[${2:dbo}].[${1:TableName}]', 'U') IS NOT NULL",
"DROP TABLE [${3:DatabaseName}].[${2:SchemaName}].[${1:TableName}]", "DROP TABLE [${2:dbo}].[${1:TableName}]",
"GO", "GO",
"-- Create the table in the specified database and schema", "-- Create the table in the specified schema",
"CREATE TABLE [${3:DatabaseName}].[${2:SchemaName}].[${1:TableName}]", "CREATE TABLE [${2:dbo}].[${1:TableName}]",
"(", "(",
"\t[${4:ColumnName}]Id INT NOT NULL PRIMARY KEY, -- Primary Key column", "\t[${3:Id}] INT NOT NULL PRIMARY KEY, -- Primary Key column",
"\t[${5:ColumnName1}] [NVARCHAR](50) NOT NULL,", "\t[${4:ColumnName2}] ${5:NVARCHAR(50)} NOT NULL,",
"\t[${6:ColumnName2}] [NVARCHAR](50) NOT NULL", "\t[${6:ColumnName3}] ${7:NVARCHAR(50)} NOT NULL",
"\t-- Specify more columns here", "\t$0-- Specify more columns here",
");", ");",
"GO" "GO"
], ],
@@ -64,10 +64,10 @@
"Drop a Table": { "Drop a Table": {
"prefix": "sqlDropTable", "prefix": "sqlDropTable",
"body": [ "body": [
"-- Drop a table called '${3:TableName}' in schema '${2:SchemaName}' in Database '${1:DatabaseName}'", "-- Drop a table called '${1:TableName}' in schema '${2:dbo}'",
"-- Drop the table if it already exists", "-- Drop the table if it already exists",
"IF OBJECT_ID('[${1:DatabaseName}].[${2:SchemaName}].[${3:TableName}]', 'U') IS NOT NULL", "IF OBJECT_ID('[${2:dbo}].[${1:TableName}]', 'U') IS NOT NULL",
"DROP TABLE [${1:DatabaseName}].[${2:SchemaName}].[${3:TableName}]", "DROP TABLE [${2:dbo}].[${1:TableName}]",
"GO" "GO"
], ],
"description": "Drop a Table" "description": "Drop a Table"
@@ -76,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}]' in database '[${4:DatabaseName}]'", "-- Add a new column '[${1:NewColumnName}]' to table '[${2:TableName}]' in schema '[${3:dbo}]'",
"ALTER TABLE [${4:DatabaseName}].[${3:SchemaName}].[${2:TableName}]", "ALTER TABLE [${3:dbo}].[${2:TableName}]",
"\tADD [${1:NewColumnName}] /*new_column_name*/ ${5:int} /*new_column_datatype*/ ${6:NULL} /*new_column_nullability*/", "\tADD [${1:NewColumnName}] /*new_column_name*/ ${4:int} /*new_column_datatype*/ ${5:NULL} /*new_column_nullability*/",
"GO" "GO"
], ],
"description": "Add a new column to a Table" "description": "Add a new column to a Table"
@@ -87,8 +87,8 @@
"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}]' in database '[${4:DatabaseName}]'", "-- Drop '[${1:ColumnName}]' from table '[${2:TableName}]' in schema '[${3:dbo}]'",
"ALTER TABLE [${4:DatabaseName}].[${3:SchemaName}].[${2:TableName}]", "ALTER TABLE [${3:dbo}].[${2:TableName}]",
"\tDROP COLUMN [${1:ColumnName}]", "\tDROP COLUMN [${1:ColumnName}]",
"GO" "GO"
], ],
@@ -98,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}]' in database '[${3:DatabaseName}]'", "-- Select rows from a Table or View '[${1:TableOrViewName}]' in schema '[${2:dbo}]'",
"SELECT * FROM [${3:DatabaseName}].[${2:SchemaName}].[${1:TableOrViewName}]", "SELECT * FROM [${2:dbo}].[${1:TableOrViewName}]",
"WHERE ${4:/* add search conditions here */}", "WHERE ${3:/* add search conditions here */}",
"GO" "GO"
], ],
"description": "Select rows from a Table or a View" "description": "Select rows from a Table or a View"
@@ -109,17 +109,17 @@
"Insert rows into a Table": { "Insert rows into a Table": {
"prefix": "sqlInsertRows", "prefix": "sqlInsertRows",
"body": [ "body": [
"-- Insert rows into table '${1:TableName}' in schema '[${2:SchemaName}]' in database '[${3:DatabaseName}]'", "-- Insert rows into table '${1:TableName}' in schema '[${2:dbo}]'",
"INSERT INTO [${3:DatabaseName}].[${2:SchemaName}].[${1:TableName}]", "INSERT INTO [${2:dbo}].[${1:TableName}]",
"( -- Columns to insert data into", "( -- Columns to insert data into",
" ${4:[ColumnName1], [ColumnName2], [ColumnName3]}", " ${3:[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",
" ${5:ColumnValue1, ColumnValue2, ColumnValue3}", " ${4:ColumnValue1, ColumnValue2, ColumnValue3}",
"),", "),",
"( -- Second row: values for the columns in the list above", "( -- Second row: values for the columns in the list above",
" ${6:ColumnValue1, ColumnValue2, ColumnValue3}", " ${5:ColumnValue1, ColumnValue2, ColumnValue3}",
")", ")",
"-- Add more rows here", "-- Add more rows here",
"GO" "GO"
@@ -130,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}]' in schema '[${2:SchemaName}]' in database '[${3:DatabaseName}]'", "-- Delete rows from table '[${1:TableName}]' in schema '[${2:dbo}]'",
"DELETE FROM [${3:DatabaseName}].[${2:SchemaName}].[${1:TableName}]", "DELETE FROM [${2:dbo}].[${1:TableName}]",
"WHERE ${4:/* add search conditions here */}", "WHERE ${3:/* add search conditions here */}",
"GO" "GO"
], ],
"description": "Delete rows from a Table" "description": "Delete rows from a Table"
@@ -141,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}]' in schema '[${2:SchemaName}]' in database '[${3:DatabaseName}]'", "-- Update rows in table '[${1:TableName}]' in schema '[${2:dbo}]'",
"UPDATE [${3:DatabaseName}].[${2:SchemaName}].[${1:TableName}]", "UPDATE [${2:dbo}].[${1:TableName}]",
"SET", "SET",
"\t[${4:ColumnName1}] = ${5:ColumnValue1},", "\t[${3:ColumnName1}] = ${4:ColumnValue1},",
"\t[${6:ColumnName2}] = ${7:ColumnValue2}", "\t[${5:ColumnName2}] = ${6:ColumnValue2}",
"\t-- Add more columns and values here", "\t-- Add more columns and values here",
"WHERE ${8:/* add search conditions here */}", "WHERE ${7:/* add search conditions here */}",
"GO" "GO"
], ],
"description": "Update rows in a Table" "description": "Update rows in a Table"
@@ -156,27 +156,27 @@
"Create a stored procedure": { "Create a stored procedure": {
"prefix": "sqlCreateStoredProc", "prefix": "sqlCreateStoredProc",
"body": [ "body": [
"-- Create a new stored procedure called '${1:StoredProcedureName}' in schema '${2:SchemaName}'", "-- Create a new stored procedure called '${1:StoredProcedureName}' in schema '${2:dbo}'",
"-- Drop the stored procedure if it already exists", "-- Drop the stored procedure if it already exists",
"IF EXISTS (", "IF EXISTS (",
"SELECT *", "SELECT *",
"\tFROM INFORMATION_SCHEMA.ROUTINES", "\tFROM INFORMATION_SCHEMA.ROUTINES",
"WHERE SPECIFIC_SCHEMA = N'${2:SchemaName}'", "WHERE SPECIFIC_SCHEMA = N'${2:dbo}'",
"\tAND SPECIFIC_NAME = N'${1:StoredProcedureName}'", "\tAND SPECIFIC_NAME = N'${1:StoredProcedureName}'",
")", ")",
"DROP PROCEDURE ${2:SchemaName}.${1:StoredProcedureName}", "DROP PROCEDURE ${2:dbo}.${1:StoredProcedureName}",
"GO", "GO",
"-- Create the stored procedure in the specified schema", "-- Create the stored procedure in the specified schema",
"CREATE PROCEDURE ${2:SchemaName}.${1:StoredProcedureName}", "CREATE PROCEDURE ${2:dbo}.${1:StoredProcedureName}",
"\t$3@param1 /*parameter name*/ int /*datatype_for_param1*/ = 0, /*default_value_for_param1*/", "\t$3@param1 /*parameter name*/ $4int /*datatype_for_param1*/ = 0, /*default_value_for_param1*/",
"\t$4@param2 /*parameter name*/ int /*datatype_for_param1*/ = 0 /*default_value_for_param2*/", "\t$5@param2 /*parameter name*/ $6int /*datatype_for_param1*/ = 0 /*default_value_for_param2*/",
"-- add more stored procedure parameters here", "-- add more stored procedure parameters here",
"AS", "AS",
"\t-- body of the stored procedure", "\t-- body of the stored procedure",
"\tSELECT @param1, @param2", "\tSELECT @param1, @param2",
"GO", "GO",
"-- example to execute the stored procedure we just created", "-- example to execute the stored procedure we just created",
"EXECUTE ${2:SchemaName}.${1:StoredProcedureName} 1 /*value_for_param1*/, 2 /*value_for_param2*/", "EXECUTE ${2:dbo}.${1:StoredProcedureName} 1 /*value_for_param1*/, 2 /*value_for_param2*/",
"GO" "GO"
], ],
"description": "Create a stored procedure" "description": "Create a stored procedure"
@@ -185,14 +185,14 @@
"Drop a stored procedure": { "Drop a stored procedure": {
"prefix": "sqlDropStoredProc", "prefix": "sqlDropStoredProc",
"body": [ "body": [
"-- Drop the stored procedure called '${1:StoredProcedureName}' in schema '${2:SchemaName}'", "-- Drop the stored procedure called '${1:StoredProcedureName}' in schema '${2:dbo}'",
"IF EXISTS (", "IF EXISTS (",
"SELECT *", "SELECT *",
"\tFROM INFORMATION_SCHEMA.ROUTINES", "\tFROM INFORMATION_SCHEMA.ROUTINES",
"WHERE SPECIFIC_SCHEMA = N'${2:SchemaName}'", "WHERE SPECIFIC_SCHEMA = N'${2:dbo}'",
"\tAND SPECIFIC_NAME = N'${1:StoredProcedureName}'", "\tAND SPECIFIC_NAME = N'${1:StoredProcedureName}'",
")", ")",
"DROP PROCEDURE ${2:SchemaName}.${1:StoredProcedureName}", "DROP PROCEDURE ${2:dbo}.${1:StoredProcedureName}",
"GO" "GO"
], ],
"description": "Drop a stored procedure" "description": "Drop a stored procedure"
@@ -241,7 +241,7 @@
"Declare a cursor": { "Declare a cursor": {
"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:dbo}'",
"DECLARE @ColumnName1 NVARCHAR(50), @ColumnName2 NVARCHAR(50)", "DECLARE @ColumnName1 NVARCHAR(50), @ColumnName2 NVARCHAR(50)",
"", "",
"DECLARE db_cursor CURSOR FOR", "DECLARE db_cursor CURSOR FOR",
@@ -304,8 +304,8 @@
"prefix": "sqlCreateIndex", "prefix": "sqlCreateIndex",
"body": [ "body": [
"-- Create a nonclustered index with or without a unique constraint", "-- 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}]'", "-- Or create a clustered index on table '[${1:TableName}]' in schema '[${2:dbo}]' 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*/", "CREATE ${5:/*UNIQUE or CLUSTERED*/} INDEX IX_${4:IndexName} ON [${3:DatabaseName}].[${2:dbo}].[${1:TableName}] ([${6:ColumnName1}] DESC /*Change sort order as needed*/",
"GO" "GO"
], ],
"description": "Create a new Index" "description": "Create a new Index"
@@ -319,13 +319,13 @@
"IF OBJECT_ID('tempDB..#${1:TableName}', 'U') IS NOT NULL", "IF OBJECT_ID('tempDB..#${1:TableName}', 'U') IS NOT NULL",
"DROP TABLE #${1:TableName}", "DROP TABLE #${1:TableName}",
"GO", "GO",
"-- Create the temporary table from a physical table called '${4:TableName}' in schema '${3:SchemaName}' in database '${2:DatabaseName}'", "-- Create the temporary table from a physical table called '${4:TableName}' in schema '${3:dbo}' in database '${2:DatabaseName}'",
"SELECT *", "SELECT *",
"INTO #${1:TableName}", "INTO #${1:TableName}",
"FROM [${2:DatabaseName}].[${3:[SchemaName}].[${4:TableName}]", "FROM [${2:DatabaseName}].[${3:[dbo}].[${4:TableName}]",
"WHERE ${5:/* add search conditions here */}" "WHERE ${5:/* add search conditions here */}"
], ],
"description": "Create a new Temporary Table" "description": "Create a new Temporary Table"
}, }
} }