What Is a TSQL Code Snippet, How Would You It, and Why?

A TSQL code snippet is a fragment of code that can easily be placed in your TSQL script using SQL Server Management Studio (SSMS). Snippets of code often contain some text that you need to modify to work for your purpose. The insert process makes it easy to identify and modify the text of that snippet. 

At a Glance

  1. Open a Query window.
  2. Insert your snippet.
  3. Create your table.
  4. Save time with a script.

By using a snippet, you can insert a block of code into an existing query window to help jump start your TSQL script writing.  Let me show you how to use a snippet that comes with SSMS to create some TSQL to create a table. 

Open a query window.

To start building my TSQL script using a snippet, I first open a query window.  I then click on the Edit menu item.  Doing this brings up a context menu , where I hover over the “Intellisense” item and finally click on “Insert Snippet…” item, as shown in the screenshot below.  Or I could use the Ctrl+K, Ctrl+X keyboard shortcut.

Start your query with a TSQL code snippet.

Insert your snippet.

After clicking the “Insert Snippet” item, or using the keyboard shortcut key (Ctrl+K,Ctrl+X) the following “Insert Snippet” window is displayed.  In this display you will find folder names, where each folder contains one or more snippets related to object type identified by the name of the folder.  For my example of how to use a snippet, I will be creating a table.  Therefore, I double click the “Table” folder to list the snippets that are associated with the tables.   

Selecting the Tables to associate with the TSQL code snippet.

After I double click the following window is displayed:

The Create Table window when using a TSQL code snippet.

Create your table.

As you can see there is only 1 snippet associate with a table item, which is called “Create Table”.  At this point all I need to do is hit enter to have the following code snippet inserted into my query window, at the position of my cursor.

TSQL code snippet and the create table command.

By looking at this code you can see some of the text is highlighted.  These highlighted items are the parts of the code that you might want to change.  The object owner item “dbo” which is highlighted in green, is the text that is ready to be changed.  I can change it by just typing in a different owner and clicking on the “Tab” key to move to the next item that could be changed.  Or I can leave the owner item alone and move on to replacing the table name by just clicking on the “Tab” key.  The text that is highlighted in green is the text that will be replaced when you start typing. 

You can also use the “Shift-Tab” key to move backwards between text items that can be changed. 

Save time with a script.

By using the “Tab” key and some typing I can quickly create the following CREATE TABLE script:

Creating a script for working with TSQL code snippet.

Snippets are a great way to quickly include fragments of code in a TSQL Script you are writing. To find out more about TSQL Snippets you can check out the Microsoft Documentation here.

 
Gregory Larsen
Gregory Larsen
Gregory A. Larsen is a DBA at Washington State Department of Health (DOH). Greg is responsible for maintaining SQL Server and other database management software. Greg works with customers and developers to design and implement database changes, and solve database/application related problems. Greg builds homegrown solutions to simplify and streamline common database management tasks, such as capacity management.

Get the Free Newsletter!

Subscribe to Cloud Insider for top news, trends & analysis

Latest Articles