SQLServerCentral Article

Using Query Analyzer Templates

,

Introduction

Query Analyzer (QA) in SQL Server 2000 was greatly enhanced over SQL Server 7.0. One of those enhancements can increase your productivity in developing code. What I am talking about are QA templates.

QA comes with over 50 canned templates that you can use. Although these templates are useful as they are installed, SQL Server exposes these templates for your modifications. Not only can you modify existing templates but you can add your own. This article will show you how to use and write your own templates. 

Using Templates

To show you how to display and use the templates that come with SQL Server let me go through a few examples of using the templates to build a TSQL script. The first thing I will show you is how to use the QA “Object Browser”.

The “Object Browser” is used to display the available QA templates. The “Object Browser” window can be brought up by clicking on the “Object Browser” toolbar icon, hitting F8, or using the “Object Browser” option under the QA “Tools” menu. When you bring up the “Object Browser” window it should look something like this:

The Object Brower window is the Left Pane of this display. At the bottom of the Object Browser window you will notice a “Template” tab. When you click on the “Template tab” a list of template directories will be displayed, like so:

QA comes with a set of standard templates. You can modify the standard set as well as build your own homegrown

templates. I’ve created a number of my own templates for doing routine database work Later on in the article I will show you how to create a homegrown template. For now lets focus on how the templates work.

To show you how to use the templates I am going to build a TSQL script using the standard supplied templates and a custom built template. The TSQL script I am going to build will create a database called DEMO_DB, define a table in this database and then perform a database backup. Not to much, just enough to show you the functionality of QA templates.

To start building this script I will first need to open a blank QA window, and make sure the Object Browser pane is open, as shown above. Next I click on the “Template” tab to show the templates directories. After this, I expand the “Create Database” folder by clicking on the “+” sign. When this is done a number of different templates will be displayed to choice from. For my script I will select the “Create Database Basic Template”. To bring the template into an empty QA window I double click on the “Create Database Basic Template”. Double clicking opens up a new QA window and insert the create database template code into this new window. My new window looks like this: 

Notice that this TSQL code is only a shell of the create database command. This shell contains parameter definitions. Parameter definitions have the following format:

<parameter_name, data_type, value>

where “parameter_name” is the name of the parameter, “data_type” is the data type of the parameter, and “value” is the default value that is to be used to replace every occurrence of the parameter in the shell, provided an alternative is not suggested. 

In the “Create Database Basic Template” there is only one parameter definition and each occurrence of this parameter looks like “<database_name, sysname, test_db>”. This parameter appears in three different places in this template. 

Now I could manually change all the parameter options in the QA editor, although that would be slow. To quickly change the parameters there is a “Replace Template Parameter” menu. This menu can be brought up by clicking on “Replace Template Parameter” option under the “Edit” menu, or by entering Ctrl+Shift+M. The “Replace Template Parameter” menu looks like this:

Note that this menu only displays one parameter. That is because the “Database Create Basic Template” only contains a single parameter. If multiple different parameters where in a template, then all parameters would be displayed on this menu. 

On the “Replace Template Parameter” menu you have 3 different columns. The ”Parameter’ column identifies the name of each parameter, the “Type” column shows what data type is associated with each parameter, and the “Value” parameter shows what value will be used as a default for each parameter. In this particular example there is one parameter “database_name”, that is of type “sysname”, and default value of “test_db”. The “Replace Template Parameter” menu retrieves each of these parameters from the code in QA’s query pane, where ever it finds a string in the pane that looks like a parameter statement (a “<” followed by a variable, followed by two commas, followed by a closing “>”).

The “Replace Template Parameter” menu is used to replace all the parameters in the QA template. For each parameter this menu allows you to take the default value or enter a value to be used instead of the default. If you want to override the default you type over the default in the value column. Once you are done entering some, or all the parameters, you need to click on the “Replace All” button. At this point the template parameter strings will all be replaced with the values you entered on the “Replace Template Parameter” menu.

Since for my example I didn’t want to take the default for the “database_name”, I typed over “test_db” and entered “DEMO_DB” into the “Replace Template Parameter” menu like so:

After I click on the “Replace All” button my QA 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

Now it is time to add onto this script with another template.

The next thing I need to do is create a table. I am going to create a table called “MyDemo”. Once again I am going to use the “Object Browser” to find a suitable “Create Table” template. I will use the “Create Table Basic Template”. Using the left mouse button I click on the “Create Table Basic Template” , but don’t release the button. Now I drag the mouse pointer over to my QA window and position my pointer just below the last “GO” command, that was created with the create database template, then release the mouse button.

This will place the template code in my QA window at the position of my mouse. Also the code is left selected. Leaving the code selected has it value, especially if my mouse happens to jump and the code gets inserted into the wrong position. By leaving it highlighted a little pointer mistake can be easy undone with the delete button, or using the cut/paste functions.

Once I get the “Create Table Basic Template” located in my script in the appropriate spot, I click some where in the QA window to un-highlight the template code. Now my code should look something 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'<table_name, sysname, test_table>' 

AND type = 'U')

DROP TABLE <table_name, sysname, test_table>

GO

CREATE TABLE <table_name, sysname, test_table> (

<column_1, sysname, c1> <datatype_for_column_1, , int> NULL, 

<column_2, sysname, c2> <datatype_for_column_2, , int> NOT NULL)

GO

Note that this template has 5 different parms: table_name, column_1, datatype_for_column_1, column_2, and datatype_for_column_2. Once again I will use the “Replace Template Parameters” menu to set the replacement values for these parameters. But first l need to determine what values I would like for each of the parameters. Here is the values I plan to use:

table_name = ‘MyDemo’

column_1 = ‘ID’

datatype_for_column_1 = ‘int’

column_2 = ‘Description’

date type_for_column_2 = ‘varchar(50)’

Now that I know what I would like for each template value, I bring up the “Replace Template Parameters” menu. Once up it looks like this:

Notice that there are 5 different parameters to enter this time, and the menu is positioning my cursor on the first parameter (table_name). I enter “MyDemo” for a table name and then hit the down arrow to go to the next parameter. For “column_1”, I enter ID, and then hit the down arrow. Now at this point I am on the ‘datatype_for_column_1” field. Now since the template has a default value for this column of ‘int’ I have two choices. Either I can enter ‘int’ or you can just take the default by using the down arrow. I’m lazy, so the down arrow works for me. Now for “column_2”, I want to enter “Description”, then the down arrow. For the “datatype_for_column_2” value I notice it defaults to “int” and I want a “varchar(50)”, so I replace the default “int” value with “varchar(50)”. 

My “Replace Template Parameter” menu looks like this after I have entered a value for each parameter:

At this point if I can hit ENTER or click on the “Replace All” button. After doing any one of these my code in QA will 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'MyDemo' 

AND type = 'U')

    DROP TABLE MyDemo

GO

CREATE TABLE MyDemo (ID int NULL, Description varchar(50) NOT NULL) 

GO

Building Custom Templates

By now you should be some what 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 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 take the default installation, in a directory call “C:\Program Files\Microsoft SQL Server\80\Tools\Templates\SQL Query Analyzer\”. In this directory there are a number of directories, one directory 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 at “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 call “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”. What ever I name the template is what will be displayed in the “Template” pane in QA, less 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 <db_name,varchar(128),DBA>

to disk = 

'<disk_name,varchar(300),C:\mssql\backup\><db_name,varchar(128),dba>_<version,varvhar(100),ADHOC>.bak'

This template contains 3 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 build “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 and 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 maybe used over and over again, consider using it as a base for building a QA template.

Rate

4 (2)

You rated this post out of 5. Change rating

Share

Share

Rate

4 (2)

You rated this post out of 5. Change rating