dcsimg
Database Journal
MS SQL Oracle DB2 Access MySQL PostgreSQL Sybase PHP SQL Etc SQL Scripts & Samples Tips Database Forum Rss Feed

» Database Journal Home
» Database Articles
» Database Tutorials
MS SQL
Oracle
DB2
MS Access
MySQL
» RESOURCES
SQL Scripts & Samples
Tips
» Database Forum
» Slideshows
Free Newsletters:



News Via RSS Feed

Rss Feed

Database Journal |DBA Support |SQLCourse |SQLCourse2
 

Featured Database Articles

Posted June 29, 2021

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

By Gregory A. Larsen

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.

 





Latest Forum Threads
DB2 Forum
Topic By Replies Updated
tables get lock during backup in db2 v9.5 Hamsoo 0 May 8th, 01:03 AM
Query to Pull Last 7 days records from a table vgoushik 0 March 20th, 06:05 PM
DB2 Visual studio addin: ANSI/Unicode problem 10Pints 0 October 5th, 02:08 AM
Things that are everbodys labor day ideas? Lipsett197 0 September 1st, 09:42 AM