Quantcast
Viewing all articles
Browse latest Browse all 3160

Create custom T-SQL code snippets with Azure Data Studio

By:Eduardo Pivaral | Last Updated: 2018-12-12 || Related Tips:More > Database Administration

Problem

If you work with SQL Server, it is very likely you have a lot of custom scripts you execute frequently. Maybe you have stored them in a folder and you open them manually as you need them or have a custom solution to load them when you load your favorite IDE. If you already have your IDE open and want to use a block of T-SQL code without opening your script files or you need to access them quickly, you need a way to load pieces of code into the IDE, this is where code snippets come into play.

Solution

Code Snippets , are blocks of reusable code that can be loaded into your work environment via a menu or a combination of hotkeys. There are several applications where you can use snippets (SSMS, VS, SSDT, Azure Data Studio to name some). Each application has a lot of built-in snippets with common code sets that you can use by default, but in this tip, we will learn how to load frequently used T-SQL code via snippets as needed. We will use Azure Data Studio IDE for this tip.

Loading Default Snippets in Azure Data Studio

If you have not used the default snippets yet, the following is the way to load them in an open document.

In a new query window, just type SQL and the list will open, after that you just must navigate with the UP/DOWN arrows to locate the snippet you want to load.


Image may be NSFW.
Clik here to view.
Create custom T-SQL code snippets with Azure Data Studio

Select the item you want to load, and press ENTER, and the code snippet will load into the window:


Image may be NSFW.
Clik here to view.
Create custom T-SQL code snippets with Azure Data Studio

Then just must replace the default values with the values you want.

Creating Custom Code Snippets

Go to View > Command Palette…


Image may be NSFW.
Clik here to view.
Create custom T-SQL code snippets with Azure Data Studio

In the new open window type snippet , and select Preferences: Configure User Snippets


Image may be NSFW.
Clik here to view.
Create custom T-SQL code snippets with Azure Data Studio

Type or locate SQL and click on it:


Image may be NSFW.
Clik here to view.
Create custom T-SQL code snippets with Azure Data Studio

If you don’t have custom snippets defined, a new sql.json window will open with commented code that you can edit:


Image may be NSFW.
Clik here to view.
Create custom T-SQL code snippets with Azure Data Studio

The basic template to add a new snippet is:

"<name of the snippet>": {
"prefix":"<sqlNameOfthePrefix>",
"body":[
"<your T-SQL code line 1 ${1:DefaultValue1}>",
"<your T-SQL code line 2 ${2:DefaultValue2}>"
"<your T-SQL code line N>"
],
"description":"<Snippet description>"
}

You can add the number of snippets you want separating each by commas.

For this tip, let's add 2 custom snippets. For the first snippet we will update all statistics on any given table:

UPDATE STATISTICS <table name>;
GO

Our first snippet will look like this:

{
"Update table statistics": {
"prefix":"sqlUpdateStatistics",
"body":[
"UPDATE STATISTICS ${1:SchemaName}.${2:TableName};",
"GO "
],
"description":"Updates all the statistics on a table"
}
}

Note that the variables are Schema Name and Table name, indicated by a ${n:defaultValue} so they are highlighted when you insert it in your code.

And the SQL.JSON file will look like this at this point:


Image may be NSFW.
Clik here to view.
Create custom T-SQL code snippets with Azure Data Studio

We will add a second snippet, this time to verify the status of a running Extended Event . The T-SQL code is this:

SELECT
ES.name,
iif(RS.name IS NULL, 0, 1) AS running
FROM sys.dm_xe_sessions RS
RIGHT JOIN sys.server_event_sessions ES ON RS.name = ES.name
WHERE es.name = '<YOUR XE SESSION NAME>'

So, the code for our second snippet will be this:

"Extended Event Status": {
"prefix":"sqlXEStatus",
"body":[
"SELECT ",
"ES.name, ",
"iif(RS.name IS NULL, 0, 1) AS running ",
"FROM sys.dm_xe_sessions RS ",
"RIGHT JOIN sys.server_event_sessions ES ON RS.name = ES.name ",
"WHERE es.name = '%${1}%'"
],
"description":"Show the status for any extended event"
}

For this one we did not define a default value for text to search, so this will check all your XE sessions.

Our SQL.JSON code looks like this:


Image may be NSFW.
Clik here to view.
Create custom T-SQL code snippets with Azure Data Studio

And this is the code:

{
"Update table statistics": {
"prefix": "sqlUpdateStatistics",
"body":[
"UPDATE STATISTICS ${1:SchemaName}.${2:TableName};",
"GO "
],
"description": "Updates all the statistics on a table"
},
"Extended Event Status": {
"prefix": "sqlXEStatus",
"body":[
"SELECT ",
"ES.name, ",
"iif(RS.name IS NULL, 0, 1) AS running ",
"FROM sys.dm_xe_sessions RS ",
"RIGHT JOIN sys.server_event_sessions ES ON RS.name = ES.name ",
"WHERE es.name = '%${1}%'"
],
"description": "Show the status for any extended event"
}
}

Now, just save it and it is ready to test in a new query window.

Testing the Custom Code Snippets in Azure Data Studio

As with the default snippets, just open a new query window and type sql , the 2 new snippets will show in the list:


Image may be NSFW.
Clik here to view.
Create custom T-SQL code snippets with Azure Data Studio

Select one of them to load into your query window:


Image may be NSFW.
Clik here to view.
Create custom T-SQL code snippets with Azure Data Studio

We can type sql again to load the second one:


Image may be NSFW.
Clik here to view.
Create custom T-SQL code snippets with Azure Data Studio

We can see the code loaded in our environment:


Image may be NSFW.
Clik here to view.
Create custom T-SQL code snippets with Azure Data Studio

Advanced Options for Code Snippets

In the previous two examples, we showed some basic options for the snippets, but you can customize them even more. I will show you some other options you can make.

Let’s go back to our first custom snippet. The table statistics update with the defaults, but you can customize the code to allow sample size and by percent or rows. Let’s go back to the SQL.JSON file and replace the update table statistics snippet with this code:

"Update table statistics": {
"prefix":"sqlUpdateStatistics",
"body":[
"UPDATE STATISTICS ${1:<Schema>}.${2:<Table>} WITH SAMPLE ${3:100} ${4|PERCENT,ROWS|};",
"GO "
],
"description":"Updates all the statistics on a table"
}

The ${3:100} variable puts the value of 100 by default, and the ${4|PERCENT,ROWS|} allows you to choose between the two values of PERCENT or ROWS when inserted, to specify more values, you must separate it by commas.

If we save the updated file and then load in a new query window and insert the

Viewing all articles
Browse latest Browse all 3160

Trending Articles