首页 > 开发 > 综合 > 正文

创建自定义模板 Building Custom Templates

2024-07-21 02:06:57
字体:
来源:转载
供稿:网友
december 23, 2002
using and building query analyzer templates
by gregory a. larsen



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 ql 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 ql 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_dbgocreate database demo_dbgo-- =============================================-- create table basic template-- =============================================if exists(select name from sysobjects where name = n'mytable' and type = 'u') drop table mytablegocreate 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_dbgocreate database demo_dbgo-- =============================================-- create table basic template-- =============================================if exists(select name from sysobjects where name = n'mytable' and type = 'u') drop table mytablegocreate 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.
注册会员,创建你的web开发资料库,
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表