分享
 
 
 

创建自定义模板 Building Custom Templates

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

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\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.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有