Getting a Jumpstart on Code with Templates and Code Snippets

Management Studio templates and Code Snippets are useful because they provide a starting point when programming new types of code, and they help make the code consistent.

Using Templates

To use a template, open the Template Explorer, select a template, and double-click it. This opens a new query in the Query Editor using code from the template.

You can also drag a template from the Template Explorer to an open Query and deposit the template code into the query.

The new query likely includes several template parameters. Rather than edit these in text, you can use a dialog box using the Query → Specify Values for Template Parameters (Ctrl+Shift+M). The dialog box has an entry for every parameter and automatically fills in the parameters within the query. If you make an error, the only way to go back is to undo each parameter change (Ctrl+Z).

Using Code Snippets

Code Snippets is a new feature to SQL Server 2012. Unlike templates, however, Code Snippets do create template parameters for you.

To activate Code Snippets, in the Query Editor window, you can either right-click the window and select Insert Snippet from the context menu, or you can use the keyboard shortcut Ctrl+K, Ctrl+X. This brings up the Code Snippet window, which enables you to choose the code snippet of your choosing via a drop-down menu. Simply double-click the options you want, and SSMS generates the T-SQL code accordingly.

Get Microsoft SQL Server 2012 Bible now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.