|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
对于update操作,只需要把event中的旧行和新行值对调即可。创立|模板December23,2002
UsingandBuildingQueryAnalyzerTemplates
ByGregoryA.Larsen
BuildingCustomTemplates
Bynowyoushouldbesomewhatfamiliarwithusingpre-existingtemplatestobuildyourSQLcode.Itisnowtimetolookathowyoucanleveragetemplatestostreamlineyouradministration.Notonlycanyouuseexistingtemplates,butyoucanalsomodifytheexistingtemplatesandbuildyourownhomegrowntemplates.
SQLServercomeswithasetoftemplateswhenyouinstallSQLServer.TheMicrosoftsuppliedtemplatesarestored(ifyouusedthedefaultinstallation)inadirectorycalled"C:ProgramFilesMicrosoftSQLServer80ToolsTemplatesQLQueryAnalyzer."Inthisdirectorythereareanumberofsubdirectories,oneforeachfolderyouseeontheQATemplatepane.IfyoudidnttakethedefaultwheninstallingSQLServer,youcanfindthetemplatedirectorybysearchingforfilesthathavea"tql"extension.
ForexamplepurposesIamgoingtocreateanewtemplatetosupportcreatingadatabasebackup.Toensurethatmyhomegrowntemplatesarestoredseparatelyfromthestandardones,Iwillbuildanewtemplatefoldertoplacemycustomtemplates.Alltemplatesyoucreatemusthavea"tql"extentioninordertoberecognizedasatemplate.
BeforeIbuildmydatabasebackuptemplate,lcreateanewdirectoryforallmyhomegrowntemplatescalled"MyTemplates."ThisnewdirectoryiscreatedunderthelocationwhereallthestandardMicrosofttemplatesarestored.Inmycase,IwillcreateanewtemplatedirectorycalledC:ProgramFilesMicrosoftSQLServer80ToolsTemplatesQLQueryAnalyzerMyTemplates."NowIamreadytobuildatemplate.
ThetemplateIamgoingtocreateiscalled"DatabaseBackupToDiskFile.tql."WhateverInamethetemplateiswhatwillbedisplayedinthe"Template"paneinQA,minusthe"tql"extention.IwilluseNOTEPADtocreatethisnewtemplatethatwillbuildasimple"BACKUPDATABASE"command.Thetemplatebuiltwilllooklikethis:
--=========================================================--Backupdatabasetodiskfile--=========================================================backupdatabasetodisk=<disk_name,varchar(300),C:mssqlackup><db_name,varchar(128),dba>_<version,varvhar(100),ADHOC>.bak
Thistemplatecontainsthreedifferentparameters.Thefirstparameterisdb_nameandisusedtoidentifythedatabasethatwillbebackedup.Asyoucansee,thisparameterisdefinedasavarchar(128),anddefaultsto"DBA."Thesecondparameterisdisk_name,whichdefaultstothestandardplacefordatabasebackupsonmymachine.Thethirdparameterprovidesawaytospecifytheversionnameforthebackup.
NowIwillputthefinaltouchesonthescriptIambuilding.SofarIhaveusedtwostandardtemplatestocreatemyscript,whichcontainsacreatedatabaseandacreatetablestatement.NowIamgoingtousemycustombuilt"DatabaseBackupToDiskFile"templatetoadda"DATABASEBACKUP"commandtotheendofmyscript.
WhenIdisplaytheQA"Template"pane,Inowseeanewfoldercalled"MyTemplates."IfforsomereasonIdontseethenewtemplate,Irightclickonthe"Templates"folderandchoosethe"Refresh"option.Noticebelowthenewlyaddedtemplatefolder"MyTemplates."
NextIclickonthe"+"signnexttothe"MyTemplate"foldertoexpand.Intheexpandedviewmynewtemplate"DatabaseBackupToDiskFile"willbedisplayed,asinthescreenshotbelow.
NowIclickontheDatabaseBackupToDiskFiletemplate,dragittotheQApane,anddropitattheendofmyQAscript.AfterIdraganddropmytemplate,myscriptlookslikethis:
--=============================================--BasicCreateDatabaseTemplate--=============================================IFEXISTS(SELECT*FROMmaster..sysdatabasesWHEREname=NDemo_DB)DROPDATABASEDemo_DBGOCREATEDATABASEDemo_DBGO--=============================================--Createtablebasictemplate--=============================================IFEXISTS(SELECTnameFROMsysobjectsWHEREname=NMyTableANDtype=U)DROPTABLEMyTableGOCREATETABLEMyTable(IDintNULL,Descriptionvarchar(50)NOTNULL)GO--=========================================================--Backupdatabasetodiskfile--=========================================================backupdatabase<db_name,varchar(128),DBA>todisk=<disk_name,varchar(300),C:mssqlackup><db_name,varchar(128),dba>_version,varvhar(100),ADHOC>.bak
Nowallthatislefttocompletemyscriptistoreplacetheparameters.OnceagainIusetheReplaceTemplateParametermenutoreplaceparameters.ThistimeIonlyneedtoenteravalueforthedb_nameparameter;Ienter"Demo_DB."FortherestoftheparametersIwilltakethedefaultvalues.Myfinalscriptlooklikethis:
--=============================================--BasicCreateDatabaseTemplate--=============================================IFEXISTS(SELECT*FROMmaster..sysdatabasesWHEREname=NDemo_DB)DROPDATABASEDemo_DBGOCREATEDATABASEDemo_DBGO--=============================================--Createtablebasictemplate--=============================================IFEXISTS(SELECTnameFROMsysobjectsWHEREname=NMyTableANDtype=U)DROPTABLEMyTableGOCREATETABLEMyTable(IDintNULL,Descriptionvarchar(50)NOTNULL)GO--=========================================================--Backupdatabasetodiskfile--=========================================================backupdatabaseDemo_DBtodisk=C:mssqlackupDemo_DB_ADHOC.bak
Conclusion
Asyoucansee,templatesareusefulforbuildingscripts.Templatescansaveyouvaluabletimewhendevelopingcode.Bybuildingyourowntemplatesandcustomizingtheexistingtemplates,youcanstreamlineyourdevelopmentprocessandeliminatesyntaxerrors.Nexttimeyouwritesomecodethatmightbeusedoverandoveragain,considerusingitasabaseforbuildingaQAtemplate.
MySQL最初的开发者的意图是用mSQL和他们自己的快速低级例程(ISAM)去连接表格。经过一些测试后,开发者得出结论:mSQL并没有他们需要的那么快和灵活。 |
|