Microsoft® SQL Server® 2012 Bible

(Ben Green) #1

106


Part I: Laying the Foundations


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 fi lls 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 win-
dow 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.

Summary ...........................................................................................................


Management Studio’s Object Explorer and Query Editor are the two primary DBA and devel-
oper interfaces for SQL Server. Mastering the navigation of both these tools is vital to your
success with SQL Server.

Following are a few key takeaways:

■ (^) Management Studio can be visually intimidating — so many windows. Close any
window not needed for the task at hand. Remove the distractions and get the job
done.
c05 July 23, 2012 11:44 AM V1
c05.indd 106c05.indd 106 7/30/2012 4:14:52 PM7/30/2012 4:14:52 PM
http://www.it-ebooks.info

Free download pdf