Create and use code snippets to quickly create Transact-SQL (T-SQL) scripts in Azure Data Studio
Code snippets in Azure Data Studio are templates that make it easy to create databases and database objects.
Azure Data Studio provides several T-SQL snippets to assist you with quickly generating the proper syntax.
User-defined code snippets can also be created.
Using built-in T-SQL code snippets
To access the available snippets, type sql in the query editor to open the list:
Select the snippet you want to use, and it generates the T-SQL script. For example, select sqlCreateTable:
Update the highlighted fields with your specific values. For example, replace TableName and Schema with the values for your database:
If the field you want to change is no longer highlighted (this happens when moving the cursor around the editor), right-click the word you want to change, and select Change all occurrences:
Update or add any additional T-SQL you need for the selected snippet. For example, update Column1, Column2, and add more columns.
Creating SQL code snippets
You can define your own snippets. To open up the SQL snippet file for editing:
Open the Command Palette (Shift+Ctrl+P), and type snip, and select Preferences: Open User Snippets:
Select SQL:
Note
Azure Data Studio inherits its code snippet functionality from Visual Studio Code so this article specifically discusses using SQL snippets. For more detailed information, see Creating your own snippets in the Visual Studio Code documentation.
Paste the following code into sql.json:
{ "Select top 5": { "prefix": "sqlSelectTop5", "body": "SELECT TOP 5 * FROM ${1:TableName}", "description": "User-defined snippet example 1" }, "Create Table snippet":{ "prefix": "sqlCreateTable2", "body": [ "-- Create a new table called '${1:TableName}' in schema '${2:SchemaName}'", "-- Drop the table if it already exists", "IF OBJECT_ID('$2.$1', 'U') IS NOT NULL", "DROP TABLE $2.$1", "GO", "-- Create the table in the specified schema", "CREATE TABLE $2.$1", "(", "$1Id INT NOT NULL PRIMARY KEY, -- primary key column", "Column1 [NVARCHAR](50) NOT NULL,", "Column2 [NVARCHAR](50) NOT NULL", "-- specify more columns here", ");", "GO" ], "description": "User-defined snippet example 2" } }
Save the sql.json file.
Open a new query editor window by clicking Ctrl+N.
Type sql, and you see the two user snippets you just added; sqlCreateTable2 and sqlSelectTop5.
Select one of the new snippets and give it a test run!
Next steps
For information about the SQL editor, see Code editor tutorial.
Feedback
https://aka.ms/ContentUserFeedback.
Coming soon: Throughout 2024 we will be phasing out GitHub Issues as the feedback mechanism for content and replacing it with a new feedback system. For more information see:Submit and view feedback for