December 23, 2002
Using and Building Query Analyzer Templates
Building Custom Templates
By now you should be somewhat familiar with using pre-existing templates to build your SQL code. It is now time to look at how you can leverage templates to streamline your administration. Not only can you use existing templates, but you can also modify the existing templates and build your own home grown templates.
SQL Server comes with a set of templates when you install SQL Server. The Microsoft supplied templates are stored (if you used the default installation) in a directory called "C:\Program Files\Microsoft SQL Server\80\Tools\Templates\SQL Query Analyzer\." In this directory there are a number of subdirectories, one for each folder you see on the QA Template pane. If you didn't take the default when installing SQL Server, you can find the template directory by searching for files that have a "tql" extension.
For example purposes I am going to create a new template to support creating a database backup. To ensure that my homegrown templates are stored separately from the standard ones, I will build a new template folder to place my custom templates. All templates you create must have a "tql" extention in order to be recognized as a template.
Before I build my database backup template, l create a new directory for all my homegrown templates called "My Templates." This new directory is created under the location where all the standard Microsoft templates are stored. In my case, I will create a new template directory called C:\Program Files\Microsoft SQL Server\80\Tools\Templates\SQL Query Analyzer\My Templates." Now I am ready to build a template.
The template I am going to create is called "Database Backup To Disk File.tql." Whatever I name the template is what will be displayed in the "Template" pane in QA, minus the "tql" extention. I will use NOTEPAD to create this new template that will build a simple "BACKUP DATABASE" command. The template built will look like this:
-- =========================================================
-- Backup database to disk file
-- =========================================================
backup database
to disk =
'<disk_name,varchar(300),C:\mssql\backup\><db_name,varchar(128),dba>_<version,varvhar(100),ADHOC>.bak'
This template contains three different parameters. The first parameter is db_name and is used to identify the database that will be backed up. As you can see, this parameter is defined as a varchar(128), and defaults to "DBA." The second parameter is disk_name, which defaults to the standard place for database backups on my machine. The third parameter provides a way to specify the version name for the backup.
Now I will put the final touches on the script I am building. So far I have used two standard templates to create my script, which contains a create database and a create table statement. Now I am going to use my custom built "Database Backup To Disk File" template to add a "DATABASE BACKUP" command to the end of my script.
When I display the QA "Template" pane, I now see a new folder called "My Templates." If for some reason I don't see the new template, I right click on the "Templates" folder and choose the "Refresh" option. Notice below the newly added template folder "My Templates."
Next I click on the "+" sign next to the "My Template" folder to expand. In the expanded view my new template "Database Backup To Disk File" will be displayed, as in the screen shot below.
Now I click on the Database Backup To Disk File template, drag it to the QA pane, and drop it at the end of my QA script. After I drag and drop my template, my script looks like this:
-- =============================================
-- Basic Create Database Template
-- =============================================
IF EXISTS (SELECT *
FROM master..sysdatabases
WHERE name = N'Demo_DB')
DROP DATABASE Demo_DB
GO
CREATE DATABASE Demo_DB
GO
-- =============================================
-- Create table basic template
-- =============================================
IF EXISTS(SELECT name
FROM sysobjects
WHERE name = N'MyTable'
AND type = 'U')
DROP TABLE MyTable
GO
CREATE TABLE MyTable (
ID int NULL,
Description varchar(50) NOT NULL)
GO
-- =========================================================
-- Backup database to disk file
-- =========================================================
backup database <db_name,varchar(128),DBA>
to disk =
'<disk_name,varchar(300),C:\mssql\backup\><db_name,varchar(128),dba>_<>version,varvhar(100),ADHOC>.bak'
Now all that is left to complete my script is to replace the parameters. Once again I use the Replace Template Parameter menu to replace parameters. This time I only need to enter a value for the db_name parameter; I enter "Demo_DB." For the rest of the parameters I will take the default values. My final script look like this:
-- =============================================
-- Basic Create Database Template
-- =============================================
IF EXISTS (SELECT *
FROM master..sysdatabases
WHERE name = N'Demo_DB')
DROP DATABASE Demo_DB
GO
CREATE DATABASE Demo_DB
GO
-- =============================================
-- Create table basic template
-- =============================================
IF EXISTS(SELECT name
FROM sysobjects
WHERE name = N'MyTable'
AND type = 'U')
DROP TABLE MyTable
GO
CREATE TABLE MyTable (
ID int NULL,
Description varchar(50) NOT NULL)
GO
-- =========================================================
-- Backup database to disk file
-- =========================================================
backup database Demo_DB
to disk =
'C:\mssql\backup\Demo_DB_ADHOC.bak'
Conclusion
As you can see, templates are useful for building scripts. Templates can save you valuable time when developing code. By building your own templates and customizing the existing templates, you can streamline your development process and eliminate syntax errors. Next time you write some code that might be used over and over again, consider using it as a base for building a QA template.