Home > Articles > Data > SQL Server

SQL Server Reference Guide

Hosted by

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

Using SQL Server Templates

Last updated Mar 28, 2003.

Almost every Database Administrator I know, as well as most software developers, has a collection of SQL Scripts and text files to help them do their job. When many of us first started down the path of working with database technologies, we were a bit overwhelmed by working with the Structured Query Language. The way SQL builds on simple statements to create complex actions is very elegant, but not always easy to immediately follow. Add in some of the less-used commands and what I had to do was find some examples and comment them heavily. I still have some of the old SQL Server version 4.2 scripts lying around.

It's not always just the syntax that I had trouble with. Sometimes I would find a really complex set of scripts that took care of all the maintenance I needed. The advantage there was that I could parse out the scripts and apply the maintenance steps as I needed to; defragmenting one index now and saving the larger ones for later. In fact, I learned a lot of what I know by dissecting scripts.

Later, when I started writing programs to move data back and forth, I once again collected scripts to do the job, this time including interesting and efficient stored procedures. I've categorized these scripts over time and cleaned them up to have the "right" indentation and format. Check with most database professionals and they will have a library that comprises their best practices and formats.

In fact, if you're working in a shop with a senior database professional, they've probably shared some of their scripts with you. If you are that senior person, it just makes life easy to hand out some of your knowledge by handing out your script library. Everyone gains more knowledge, and your best practices pervade your workplace. Having a standard set of scripts makes it easy to read the code everyone creates, and makes finding bugs easier.

I've used a lot of methods to track, store and share my scripts over the years. But recently I've been using the tools built right in to SQL Server to store the files. In fact, the tools provide not only a place to store your own files, but Microsoft provides lots of scripts of their own. Microsoft calls this collection of scripts "Templates." These templates are not just SQL Scripts; they have special characters that mark parameters for the items you need to replace. I'll walk you through an exercise on that in a moment.

In SQL Server 2000, the template function is in the Query Analyzer tool. If you want to follow along with this example, open Query Analyzer and connect to a server. Once inside, if your Object Browser isn't showing, press F8 to bring it up. You'll see two tabs at the bottom of the Object Browser once you do: one for objects, and the other for the templates. Click the templates tab.

You'll see quite a few templates, which are sorted into folders that have categories as names. For this example, open the "Create Table" folder and select the first template you see there, called "Create Table Basic Template." Double-click that and you'll see the text for the basic outline for creating a table.

The template contains everything you need to create a table, including the script that checks for its existence, and drops it if it is already there. The template includes characters, however, that will give you an error if you run it as it looks now. You need to remove the text between the angle-brackets ( < and > ) and replace it with the variables you want to create a table. You can do that by selecting the text and typing over it, but Microsoft provides an easier way. Press CTRL-SHIFT-M to bring up a dialog box that allows you to type in the parameters that need to be replaced.

In this example I've replaced the parameters with a table name of "Clients" and two columns of the varchar type. Pressing the Replace All button walks through the script and cleans it up.

The scripts Microsoft provides are very good and contain a lot of great programming practices. Chances are they won't match what you do at your shop. You can create and save your own scripts, with or without parameters. To create your own scripts as templates, just create them as a standard text file with the .sql extension and save them in the templates directory. For SQL Server 2000, the scripts are stored by default in C:\Program Files\Microsoft SQL Server\80\Tools\Templates. You'll find three directories underneath that: one for DTS, another for SQL Profiler and the other for SQL Query Analyzer. This last directory is where the scripts are located that we've been looking at.

If you want to include parameters in your templates, add the angle-brackets and include three elements: the parameter name, the type, and the parameter value, like this: <TableName, systype, EnterNameHere>. You can actually enter a comma if there is no system type, like this: <TableName, , EnterNameHere>. Then your users will be able to replace them just like the ones included with SQL Server.

In SQL Server 2005, Microsoft has improved this feature even further. You can access the Templates by opening SQL Server Management Studio, and selecting the View and then Template Explorer menu items.

When you open this view in Management Studio you'll find even more templates than in SQL Server 2000. In this example I've drilled down to the Create Table template and opened it up. Replacing the parameters works the same way, by pressing CTRL-SHIFT-M.

Just as in SQL Server 2000, the templates for SQL 2005 are text files located in a subdirectory. You can find those (by default) in C:\Program Files\Microsoft SQL Server\90\Tools\Binn\VSShell\Common7\IDE\sqlworkbenchprojectitems. There are four subdirectories this time, one for Analysis Services, one for connections, another for T-SQL queries, and one for the CE edition of SQL Server. You don't have to access this directory if you don't want to, because you can right-click on any folder and you can create your own templates from there.

In both SQL Server 2000 and 2005, I normally make a subdirectory off of the main template area called "Microsoft." I move the template files that ship with SQL Server there, and then copy my set to the base directory, sorted into directories that match the categories I care about. I've even set up a batch file to copy the files from a network directory each time a DBA logs in to their machine.

Informit Articles and Sample Chapters

You don't have to use templates in the graphical tools. You can pass scripts in from the command line, and this book excerpt shows you how.

Online Resources

Need some scripts to get started? My friends over at SQL Server Central have some of the best around.