- Table of Contents
- Microsoft SQL Server Defined
- Microsoft SQL Server Features
- Microsoft SQL Server Administration
- Microsoft SQL Server Programming
- An Outline for Development
- Database Services
- Database Objects: Databases
- Database Objects: Tables
- Database Objects: Table Relationships
- Database Objects: Keys
- Database Objects: Constraints
- Database Objects: Data Types
- Database Objects: Views
- Database Objects: Stored Procedures
- Database Objects: Indexes
- Database Objects: User Defined Functions
- Database Objects: Triggers
- Database Design: Requirements, Entities, and Attributes
- Business Process Model Notation (BPMN) and the Data Professional
- Business Questions for Database Design, Part One
- Business Questions for Database Design, Part Two
- Database Design: Finalizing Requirements and Defining Relationships
- Database Design: Creating an Entity Relationship Diagram
- Database Design: The Logical ERD
- Database Design: Adjusting The Model
- Database Design: Normalizing the Model
- Creating The Physical Model
- Database Design: Changing Attributes to Columns
- Database Design: Creating The Physical Database
- Database Design Example: Curriculum Vitae
- The SQL Server Sample Databases
- The SQL Server Sample Databases: pubs
- The SQL Server Sample Databases: NorthWind
- The SQL Server Sample Databases: AdventureWorks
- The SQL Server Sample Databases: Adventureworks Derivatives
- UniversalDB: The Demo and Testing Database, Part 1
- UniversalDB: The Demo and Testing Database, Part 2
- UniversalDB: The Demo and Testing Database, Part 3
- UniversalDB: The Demo and Testing Database, Part 4
- Getting Started with Transact-SQL
- Transact-SQL: Data Definition Language (DDL) Basics
- Transact-SQL: Limiting Results
- Transact-SQL: More Operators
- Transact-SQL: Ordering and Aggregating Data
- Transact-SQL: Subqueries
- Transact-SQL: Joins
- Transact-SQL: Complex Joins - Building a View with Multiple JOINs
- Transact-SQL: Inserts, Updates, and Deletes
- An Introduction to the CLR in SQL Server 2005
- Design Elements Part 1: Programming Flow Overview, Code Format and Commenting your Code
- Design Elements Part 2: Controlling SQL's Scope
- Design Elements Part 3: Error Handling
- Design Elements Part 4: Variables
- Design Elements Part 5: Where Does The Code Live?
- Design Elements Part 6: Math Operators and Functions
- Design Elements Part 7: Statistical Functions
- Design Elements Part 8: Summarization Statistical Algorithms
- Design Elements Part 9:Representing Data with Statistical Algorithms
- Design Elements Part 10: Interpreting the Data—Regression
- Design Elements Part 11: String Manipulation
- Design Elements Part 12: Loops
- Design Elements Part 13: Recursion
- Design Elements Part 14: Arrays
- Design Elements Part 15: Event-Driven Programming Vs. Scheduled Processes
- Design Elements Part 16: Event-Driven Programming
- Design Elements Part 17: Program Flow
- Forming Queries Part 1: Design
- Forming Queries Part 2: Query Basics
- Forming Queries Part 3: Query Optimization
- Forming Queries Part 4: SET Options
- Forming Queries Part 5: Table Optimization Hints
- Using SQL Server Templates
- Transact-SQL Unit Testing
- Index Tuning Wizard
- Unicode and SQL Server
- SQL Server Development Tools
- The SQL Server Transact-SQL Debugger
- The Transact-SQL Debugger, Part 2
- Basic Troubleshooting for Transact-SQL Code
- An Introduction to Spatial Data in SQL Server 2008
- Performance Tuning
- Practical Applications
- Professional Development
- Application Architecture Assessments
- Business Intelligence
- Tips and Troubleshooting
- Additional Resources
Using SQL Server Templates
Last updated Aug 4, 2006.
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.
Need some scripts to get started? My friends over at SQL Server Central have some of the best around.