Create your own snippets of SQL code, and insert them into your queries
Part three of a four-part series of blogs

With SQL Server 2012 came the power not just to use pre-defined code snippets, but also to create your own - this blog explains how.

  1. Creating your own snippets of SQL
  2. Three examples of the use of snippets
  3. Creating custom snippets using Snippets Generator (this blog)
  4. Importing SQL Snippets into Management Studio

Posted by Andy Brown on 23 November 2017

You need a minimum screen resolution of about 700 pixels width to see our blogs. This is because they contain diagrams and tables which would not be viewable easily on a mobile phone or small laptop. Please use a larger tablet, notebook or desktop computer, or change your screen resolution settings.

Creating custom snippets using Snippets Generator

Having seen how useful snippets can be, how do you go about creating them?

Installing the necessary software

You can download Snippets Generator from SQLArtBits:

Downloading Snippets Generator

At the time of writing, this is the link that you can click on to download the software.

 

You can then choose one of the servers to use:

Choose a server

Presumably it doesn't make much difference which download server you choose.

The rest of the installation is pretty standard.

Creating a snippet

To add a new snippet, run the Snippets Generator application, then choose the tab shown below:

Creating a new snippet

Click on the New Snippet Details tab as shown here.

 

You can now fill in the details for your snippet:

Snippet details

The most important choice is the snippet type: do you want it to be a standard one, or to surround text that you have selected?

Now select some SQL in Management Studio:

Standard SQL syntax

This snippet is just the bare bones of a SELECT statement.

 

Paste this into Snippets Generator:

Pasting SQL

You won't see any formatting for your SQL in this view, but obviously it will come up when your paste your snippet into Management Studio.

 

Now save your snippet:

Saving a snippet

Click on this button at the bottom right corner of your dialog box to save this snippet.

 

Give this snippet a name, and choose where to store it:

Naming a snippet

Here I've put this snippet in the same folder as all of the others, for reasons that will become apparent soon.

Special considerations for SurroundText snippets

If your snippet is designed to surround SQL that you have selected, be careful not to remove the central marker:

Surround snippet

For this type of snippet you can put anything you like before and after the $selected$ $end$ text; just be careful not to edit or delete the text itself.

Storing your snippets

The best place to put your snippets is in a new folder at the root level of your C drive:

Snippets folder

By putting this Wise Owl snippety things folder at the root level of your hard disk, SQL Server Management Studio won't create a folder path for it when adding a snippet.

 

The next and final step of this blog shows how to import your snippets into Management Studio.

This blog has 0 threads Add post