Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 50 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
50
Dung lượng
1,41 MB
Nội dung
a disabled t rigger, all y ou have t o do is enable it . Recall t hat you can disable and enable a t r igger w it h an ALTER TABLE st at em ent for the t able wit h t he t rigger that you w ant to disable t em porarily. The follow ing script dem onst rat es t he syntax for cr eat ing a trigger for t he MyTable table cr eat ed earlier in t his chapt er. ( See t he “Creat ing a Scalar UDF Wit hout Param et ers” sect ion.) The trigger prot ect s the table from insert s, updat es, and delet es by rolling back t he t ransact ion associat ed w it h t he tr igger. The script st art s by rem oving any previous version of t he trgKeepMyTableUnt ouched tr igger and t hen begins a CREATE TRI GGER st at em ent. Lik e m ost ot her CREATE st at em ents, t he CREATE TRI GGER st at em ent m ust occur at the t op of a bat ch. Therefore, the code to drop the old version ends wit h t he GO keyword. The ON clause of t he CREATE TRIGGER st at em ent designat es the MyTable t able as the one t o w hich the t rigger will belong. The FOR clause indicates t hat the t rigger will fir e for insert , updat e, and delet e ev ents. The first st at em ent aft er t he AS keyword is a RAI SERROR st at em ent that sends a cust om m essage back t o t he Messages pane of Query Analyzer. An inform at ional m essage issued from a trigger is useful for let t ing a user k now t hat a t rigger fired. The RAI SERROR st atem ent can serve ot her funct ions as well, but it is a robust alt ernat iv e t o t he PRI NT st at em ent for sending m essages t o t he Messages pane. The st ring for a cust om m essage can be up to 400 charact ers. The trailing values 16 and 1 indicat e t he severity and st at e for t he err or. For sim ple inform at ional m essages, you can consist ent ly apply these values. The second T- SQL st at em ent in t he scr ipt rolls back t he t ransact ion to m odify the table. The ROLLBACK TRAN st at em ent is an abbrev iat ed version of the ROLLBACK TRANSACTI ON st at em ent. I n eit her form , t his st at em ent rem oves any insert ed rows, rest ores any colum n values to t heir nonupdat ed st at e, and adds back any delet ed r ows. You w ill generally want t o use t he ROLLBACK TRAN st at em ent as the last st at em ent in a t rigger because any st at em ents after ROLLBACK TRAN can m odify the t able for a t rigger. --trgKeepMyTableUntouched --Drop prior version of trigger. IF EXISTS (SELECT name FROM sysobjects WHERE name = ’trgKeepMyTableUntouched’ AND type = ’TR’) DROP TRIGGER trgKeepMyTableUntouched GO --Create new trigger to keep MyTable table untouched. CREATE TRIGGER trgKeepMyTableUntouched ON MyTable FOR INSERT, UPDATE, DELETE AS RAISERROR(‘Message from trgKeepMyTableUntouched.’,16,1) ROLLBACK TRAN GO The follow ing script is a collect ion of T-SQL stat em ents t hat dem onst rat es t he behavior of t he t rigger as well as how t o disable and rest ore the t r igger. The first couple of bat ches in t he script at t em pt t o delet e all rows from t he MyTable table and m odify a colum n value in t he table. Neither bat ch succeeds because t he trgKeepMyTableUnt ouched tr igger prot ect s t he MyTable table fr om delet e and updat e events (as well as insert events) . I f it becom es essential t o m odify a t able wit h a trigger that blocks changes, y ou can t em porar ily disable the t r igger. The script dem onst rates t he syntax for t he trgKeepMyTableUnt ouched tr igger. You have to m odify the My Table table wit h t he ALTER TABLE stat em ent to disable it s t rigger. After disabling t he trigger, the script changes t he m axim um value in t he col1 colum n. Then, in anot her bat ch, the script rest ores the init ial m ax im um value. The scripts use a scalar UDF developed ear lier in t his chapter to accom plish t hese task s. After successfully Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. m odifying the t able wit h t he tr igger disabled, t he script enables t he t rigger again for t he MyTable table with t he ALTER TABLE st atem ent . Just to confirm t he trigger’s operat ion, the script again at t em pt s t o delet e all rows fr om t he t able. The trigger fires and prints it s inform at ional m essage and rolls back the transact ion t o rem ov e t he r ows from t he t able. --Demo_trgKeepMyTableUntouched --An attempt to delete all records fails with --trigger error message. DELETE FROM MyTable GO --An attempt to update the maximum value in --col1 in the MyTable table fails also. UPDATE MyTable SET col1 = dbo.udfOneHigherThanMax() WHERE col1 = (SELECT MAX(col1) FROM MyTable) GO --Disable the trigger for MyTable without dropping it. ALTER TABLE MyTable Disable TRIGGER trgKeepMyTableUntouched GO --Update attempt for MyTable succeeds. UPDATE MyTable SET col1 = dbo.udfOneHigherThanMax() WHERE col1 = (SELECT MAX(col1) FROM MyTable) SELECT * FROM MyTable GO --Restoring update event also succeeds. UPDATE MyTable SET col1 = dbo.udfOneHigherThanMax() - 2 WHERE col1 = (SELECT MAX(col1) FROM MyTable) SELECT * FROM MyTable GO --Re-enable trigger. ALTER TABLE MyTable Enable TRIGGER trgKeepMyTableUntouched GO --An attempt to delete all records fails again --with trigger error message. DELETE FROM MyTable GO Ar chiving Changes t o a Table The logical t ables insert ed and delet ed contain t he ch anges t hat users m ake t o a table. Unfort unat ely, the insert ed and delet ed t ables are available only for t he tim e that a t rigger has control of an applicat ion. When t he tr igger closes, SQLServer in effect clears t he tables. I f you want t o persist som e su bset of t he changes t o a t able for perm anent ready access, you can use triggers t o save the cont ent s of the logical insert ed and delet ed t ables to a t able in a SQLServer Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. dat abase. Because changes ( insert s, updat es, and delet es) affect t he insert ed and delet ed t ables differently, one approach is t o cr eat e a separat e t rigger for each type of change. This sim plifies t he trigger logic, and it m akes each type of change run fast er t han having one trigger t hat deciphers t he t ype of change and t hen archives t he insert ed and delet ed t ables properly. The follow ing script creat es three tr iggers to log insert s, updat es, and delet es to the MyTable t able in the ChangeLogForMyTable table. The script st art s by rem oving t he trgKeepMy TableUnt ouched t rigger creat ed in t he pr ev ious sam ple. Recall that t he previous t rigger block s all changes t o t he MyTable t able. Next this procedur e cr eat es a fresh blank version of the ChangeLogForMy Table table. The table has four colum ns— one for t he col1 values from t he inserted or delet ed t able, a second for t he t ype of change, a t hird for the dat e and tim e of the change, and a fourt h colum n for t he login of the user m aking t he change. Aft er cr eat ing a table t o arch iv e ch anges, the script creat es a fresh copy of the trgI nsert ToChangeLog t rigger. This t r igger copies t he col1 value from t he inserted t able to a local var iable. Then it uses the local variable in t he VALUES clause of an I NSERT I NTO st at em ent to persist the new value to t he ChangeLogForMy Table t able. The script uses a st ring const ant— I NSERT—t o designat e t he type of change. The CURRENT_TI MESTAMP and SYSTEM_USER key words denot e built- in funct ions that r et urn t he current dat e and t im e as w ell as the login for t he current user (t he one who m ak es the change) . The CREATE TRIGGER st at em ents for the t rgDelet eToChangeLog and trgUpdat eToChangeLog triggers persist t he delet e and updat e col1 values t o t he ChangeLogForMyTable table. When logging delet es, you use t he delet ed t able inst ead of t he insert ed t able. I n t he case of updat es, you log t he cont ent s of t he delet ed and insert ed t ables t o t he ChangeLogFor My Table table. However, the basic design of delet e and updat e t riggers cor resp onds to t he trgI nsert ToChangeLog trigger. --trgInsertUpdateDeleteToChangeLog --Drop prior version of trgKeepMyTableUntouched trigger. IF EXISTS (SELECT name FROM sysobjects WHERE name = ’trgKeepMyTableUntouched’ AND type = ’TR’) DROP TRIGGER trgKeepMyTableUntouched GO --Remove prior version of ChangeLogForMyTable table. IF EXISTS(SELECT TABLE_NAME = ’ChangeLogForMyTable’ FROM INFORMATION_SCHEMA.TABLES) DROP TABLE ChangeLogForMyTable --Create ChangeLogForMyTable table. CREATE TABLE ChangeLogForMyTable ( col1 int, type varchar (10), changedatetime datetime, changeuser varchar(128) ) GO --Drop prior version of trgInsertToChangeLog trigger. IF EXISTS (SELECT name FROM sysobjects WHERE name = ’trgInsertToChangeLog’ AND type = ’TR’) DROP TRIGGER trgInsertToChangeLog GO --Create trigger to monitor inserts. CREATE TRIGGER trgInsertToChangeLog ON MyTable Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. FOR INSERT AS DECLARE @col1value int SET @col1value = (SELECT col1 FROM inserted) INSERT INTO ChangeLogForMyTable VALUES(@col1value, ’INSERT’, CURRENT_TIMESTAMP, SYSTEM_USER) GO --Drop prior version of trgDeleteToChangeLog trigger. IF EXISTS (SELECT name FROM sysobjects WHERE name = ’trgDeleteToChangeLog’ AND type = ’TR’) DROP TRIGGER trgDeleteToChangeLog GO --Create trigger to monitor deletes. CREATE TRIGGER trgDeleteToChangeLog ON MyTable FOR DELETE AS DECLARE @col1value int SET @col1value = (SELECT col1 FROM deleted) INSERT INTO ChangeLogForMyTable VALUES(@col1value, ’DELETE’, CURRENT_TIMESTAMP, SYSTEM_USER) GO --Drop prior version of trgUpdateToChangeLog trigger. IF EXISTS (SELECT name FROM sysobjects WHERE name = ’trgUpdateToChangeLog’ AND type = ’TR’) DROP TRIGGER trgUpdateToChangeLog GO CREATE TRIGGER trgUpdateToChangeLog ON MyTable FOR UPDATE AS DECLARE @col1value int SET @col1value = (SELECT col1 FROM deleted) INSERT INTO ChangeLogForMyTable VALUES(@col1value, ’UPDATE’, CURRENT_TIMESTAMP, SYSTEM_USER) SET @col1value = (SELECT col1 FROM inserted) INSERT INTO ChangeLogForMyTable VALUES(@col1value, ’UPDATE’, CURRENT_TIMESTAMP, SYSTEM_USER) GO The follow ing script should be run im m ediat ely after you creat e t he t r iggers wit h the preceding scr ipt . I t also benefits from a fresh copy of t he MyTable t able, such as the one generat ed by t he udfHigherThanMax script in t he “Creat ing a Scalar UDF Wit hout Param et ers” sect ion. Th e script m akes a ser ies of changes to the MyTable table. Aft er each change, it uses SELECT st at em ents t o ret urn t he MyTable table and t he ChangeLogForMy Table table. The first change is t o add a new row wit h t he value 25 for col1. Next it updat es t he value 25 to 26. Finally it delet es the row in t he My Table table w it h a col1 value of 26. --Demo_trgInsertUpdateDeleteToChangeLog --Insert a new row into MyTable and display --MyTable and ChangeLogForMyTable tables INSERT INTO MyTable (col1) VALUES (25) SELECT * FROM MyTable SELECT * Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. FROM ChangeLogForMyTable GO --Update inserted row value and display --MyTable and ChangeLogForMyTable tables. UPDATE MyTable SET col1 = 26 WHERE col1 = 25 SELECT * FROM MyTable SELECT * FROM ChangeLogForMyTable GO --Delete updated row and display --MyTable and ChangeLogForMyTable tables. DELETE FROM MyTable WHERE col1 = 26 SELECT * FROM MyTable SELECT * FROM ChangeLogForMyTable GO Exam ining the Result s pane contents will allow you t o follow t he changes t o t he MyTable table as well as t he ChangeLogForMyTable table. The first display of the ChangeLogForMyTable table sh ows a table with j ust one row and a col1 value of 25. I n t he next display of t he table, y ou can see t hree r ows. This is because an updat e adds t w o rows t o t he table. I n its final appearance in t he results pane, the ChangeLogForMyTable table contains four rows. Enforcing a Business Rule on a Table One of t he classic uses for triggers is t he enforcem ent of business rules. Aft er all, the t rigger always fires befor e a change event. The T- SQL in t he tr igger can assess t he change to m ake sure it conform s t o business rules before com m it t ing the change t o a table. I f a change value doesn’t sat isfy a business rule, t he trigger can t ake an appropriat e rem edy, such as rej ect ing t he change or rev ising the change and inform ing t he user of any r em edial act ion. The next sam ple enforces a sim ple business rule. The rule is t hat users can insert only even num bers int o col1 of t he My Table t able. Your norm al business rules can be subst antially m or e sophist icat ed t han t his sam ple, but t he triggers t o enforce those rules can st ill use t he sam e logic. First you t est the change value t o m ake sure it adheres to the rule. Second, if t he change value doesn’t confor m to the business rule, your tr igger can perform an appropriat e rem edial act ion for t he invalid change value. Third, if t he change value sat isfies t he business rule, you insert it int o the table. N ot e Before running the sam ple script in t his sect ion, m ake sure you drop all other tr iggers for the MyTable t able that can conflict wit h t he sam ple below. The sam ple script on t he book’s com panion CD rem oves all prior t riggers cr eated for Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. the MyTable table in t his chapter. For brevit y, t he list ing here doesn’t show the code for dropping all t hese triggers. The sam ple uses an I NSTEAD OF tr igger. Because t his t ype of tr igger fir es before the change event, t here is no need t o roll back a t ransact ion for an invalid act ion. The sam ple uses the m odulo operat or ( % ) to check whet her a num ber divides evenly by 2. A rem ainder of 1 indicates an odd num ber. This outcom e calls for a rem edial act ion. The act ion in t his inst ance is t o add 1 t o the input value from the insert ed t able, const ruct a m essage indicating t he alt ernat iv e act ion t ak en, and finally insert the new even num ber int o t he table. A rem ainder of 0 indicat es an even num ber. Because ev en num bers sat isfy the business rule, t he t rigger can j ust insert t he value from the insert ed table int o col1 of t he MyTable table. Aft er t he creat ion of t he tr igger, t he script includes data m anipulat ion and SELECT st at em ents t o t est the t rigger’s logic. You can run t he sam ple script and see the trigger aut om at ically add 1 when the script at t em pts to input an odd num ber (25) int o col1 in t he MyTable t able. On t he ot her hand, t he trigger m erely accept s t he insert of an even num ber ( 24) into col1 in the MyTable table. --trgInsteadOfInsert --Drop prior version of trgInsteadOfInsert trigger. IF EXISTS (SELECT name FROM sysobjects WHERE name = ’trgInsteadOfInsert’ AND type = ’TR’) DROP TRIGGER trgInsteadOfInsert GO --Create an INSTEAD OF trigger. CREATE TRIGGER trgInsteadOfInsert ON MyTable INSTEAD OF INSERT AS DECLARE @col1value int DECLARE @newcol1value int DECLARE @strMsg varchar(400) SET @col1value = (SELECT col1 FROM inserted) --If inserted value is odd, make it even --before inserting it. IF @col1value%2 = 1 BEGIN SET @newcol1value = @col1value + 1 SET @strMsg = ’The value you want to insert is: ’ + CAST(@col1value AS varchar(3)) + ’, but it violates a business rule.’ + CHAR(10) + ’ Therefore, I insert ’ + CAST(@newcol1value AS varchar(3)) + ’.’ RAISERROR (@strMsg,16,1) INSERT INTO MyTable (col1) VALUES(@newcol1value) END ELSE INSERT INTO MyTable (col1) VALUES(@col1value) GO --Try to insert an odd value into col1 in MyTable. INSERT INTO MyTable (col1) VALUES(25) --Display the col1 values in MyTable. SELECT * FROM MyTable --Delete the next even value after the odd value. Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. DELETE FROM MyTable WHERE col1 = 26 --Display the col1 values in MyTable. SELECT * FROM MyTable --Insert an even value into col1 in MyTable. INSERT INTO MyTable (col1) VALUES(24) --Display the col1 values in MyTable. SELECT * FROM MyTable --Delete the new even col1 value in MyTable. DELETE FROM MyTable WHERE col1 = 24 --Display the col1 values in MyTable. SELECT * FROM MyTable Enforcing a Business Rule on a View Two of the advantages of views are that they perm it you to insulat e your dat abase schem a from t he user int erface for an application and t hat you can select ively expose subset s from a table wit hout exposing all the dat a in a base table. These feat ures perm it you to secure t he base table or tables for a view from all or m ost users while you grant these sam e users access t o a subset of t he dat a from the base table or tables through a view. Unfortunat ely, AFTER t riggers never applied t o views, so previously you couldn’t enforce business rules wit h triggers for views. SQLServer2000 intr oduced I NSTEAD OF t riggers, w hich apply to views. Therefor e, you can gain the benefit s of ex posing dat a t hr ough view s and st ill be able t o enforce business rules via tr iggers. The sam ple in t his sect ion dem onst rat es the syntax for applying a business rule for insert s int o a view . The v iew is vewMy Table. This view ret urns all t he rows for the colum n in t he MyTable table. The business rule is that t he insert ed col1 v alue can be only 1 great er t han t he current m axim um in col1 of t he MyTable table. N ot e As wit h the sam ple script from t he preceding sect ion, you should rem ove all t r iggers t hat can conflict wit h t he new trigger. The version of the following sam ple on t he book ’s com panion CD rem oves all prior triggers creat ed for t he MyTable table in this chapter. For brevit y, the list ing here doesn’t show the code for dropping all t hese triggers. The script below st arts wit h t he cr eat ion of the vew MyTable v iew. Then the scr ipt m oves on t o cr eat e a fresh v ersion of trgI nst eadOfI nsert For vewMyTable. No special act ion is necessary for creat ing a trigger for a view. I n t he ON clause for the CREATE TRI GGER st at em ent , j ust nam e t he view— vew MyTable, in t his case. The trigger’s logic uses t he udfOneHigherThanMax UDF creat ed earlier in t his chapter . You should run the code t o creat e t his UDF if it isn’t available. The logic for enforcing t he business rule is t he sam e as for the previous t rigger, alt hough Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. the act ual business r ule is different. An I F…ELSE st at em ent t est s for t he validit y of t he new value r elat iv e to t he business rule. I f the new value fails the test , the trigger perform s a rem edial act ion. This act ion print s a m essage let t ing the user know t he new value is invalid. Because the t rigger is an I NSTEAD OF tr igger, there is no need t o roll back t he insert . I f t he new value is valid, t he t rigger inserts the new value into vewMyTable. Aft er t he script creat es the t rigger, t he script goes on to test t he tr igger by trying to insert two new values. The first value violat es t he business rule, and t he trigger rej ect s it . The second value sat isfies t he business rule, and t he t rigger inserts the new value into col1 of t he MyTable t able. The final dat a m anipulat ion st at em ent in t he scr ipt rem oves t he value new ly insert ed into t he vew MyTable view t o rest ore the base table to it s init ial st at e. --trgInsteadOfInsertForvewMyTable --Drop prior version of vewMyTable view. IF EXISTS(SELECT TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME = ’vewMyTable’) DROP VIEW vewMyTable GO --Create vewMyTable view. CREATE VIEW vewMyTable AS SELECT * FROM MyTable GO --Drop prior version of trgInsteadOfInsertForvewMyTable trigger. IF EXISTS (SELECT name FROM sysobjects WHERE name = ’trgInsteadOfInsertForvewMyTable’ AND type = ’TR ’) DROP TRIGGER trgInsteadOfInsertForvewMyTable GO --Create an INSTEAD OF trigger for a view. CREATE TRIGGER trgInsteadOfInsertForvewMyTable ON vewMyTable INSTEAD OF INSERT AS DECLARE @col1value int SET @col1value = (SELECT col1 FROM inserted) IF @col1value > dbo.udfOneHigherThanMax() RAISERROR(‘Value too high.’,17,1) ELSE INSERT INTO vewMyTable (col1) VALUES(@col1value) GO --Attempting to insert a value of 100 fails --through vewMyTable. INSERT INTO vewMyTable (col1) VALUES(100) SELECT * FROM vewMyTable GO --Attempting to insert a value one higher --than the maximum value succeeds. INSERT INTO vewMyTable (col1) VALUES(dbo.udfOneHigherThanMax()) SELECT * FROM vewMyTable GO Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. --Remove inserted value. DELETE FROM vewMyTable WHERE col1 = dbo.udfOneHigherThanMax()-1 GO Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. Cha pt er 6 . SQL Se rve r 2 0 0 0 XM L Functiona lit y When MicrosoftSQLServer2000 was launched, Microsoft com m itt ed it self t o providing t he best Ext ensible Mark up Language ( XML) funct ionalit y possible. XML is im portant because it prom ises to revolut ionize the way dat abase and Web developers im plem ent dat a access and dat a m anipulat ion capabilit ies in their solut ions. Micr osoft said it would revise t he init ial release wit h t im ely updat es t hat included new funct ionalit y reflect ing t he rapidly evolv ing XML st andards and relat ed developm ent issues. As this chapter was being pr epared, Microsoft delivered on it s com m it m ent wit h the release of it s lat est updat e— t he MicrosoftSQL Ser ver 2000 Web Services Toolk it. The toolk it follow s t wo earlier releases: XML for SQLServer2000 Web Release 1 and XML for SQLServer2000 Web Release 2. The Web Serv ices Toolkit is based on SQLXML 3.0 and includes t he SQLXML 3.0 inst allat ion package. Microsoft says t hat t he feat ures int roduced in SQLXML 1.0 and SQLXML 2.0 are included in t he SQLXML 3.0 package. See Chapt er 12 for coverage of t he com pat ibilit y of t he t oolkit w it h t he t wo prior Web r eleases. I n addit ion, see Cha pt er 1 3 for com m entary and sam ples using t he Web Serv ices Toolk it. You w ill gain from t his chapter an ov erall under st anding of XML funct ionalit y in SQLServer wit h an em phasis on access to t hat funct ionalit y via T- SQL, XML schem as and t em plat es, and hypert ext t ranspor t prot ocol (HTTP) . Chapter 12 will refocus on XML so that you can build on the underst anding present ed her e while you learn how t o tap the XML capabilit ies in SQL Serv er wit h VisualBasic.NET and relat ed t echnologies, such as ADO.NET. Wit h XML, developers can build incr edibly pow erful solut ions for ret rieving and m aint aining dat a ov er Web connect ions. As the word get s out about how easy it is to creat e t hese solutions, you will becom e an evangelist for using XML wit h SQL Ser ver. This ch apt er relies on t he Nort hwind sam ple dat abase. The chapter sam ples add a couple of new v iews and user-defined funct ions to t he dat abase for use wit h XML files. T- SQL script s for creat ing t hese obj ect s are included wit h the sam ple files for t his chapt er. The m ain r esource for the chapt er is a collect ion of nearly 20 XML files along w it h an assort m ent of URLs. Som e of t he URLs dem onst rat e direct access t o a SQLServer dat abase, while ot her URLs invok e an XML file and access a SQLServer database indir ect ly t hrough t he XML file. Overview of XM L Support I n learning about XML funct ionalit y, it is im port ant to recall t hat Micr osoft int r oduced XML processing power t o SQLServer2000 in m ult iple waves. This m eans that selected XML feat ures available from t he init ial version of SQL Ser ver 2000 have been obsolet ed, or at least deprecated, by subsequent ly intr oduced XML t echniques. This is because Web Release 1 and Web Release 2— and now t he Web Serv ices Toolkit — added new XML funct ionalit y not available in t he init ial release. The overv iew of XML capabilit ies in t his sect ion has t wo parts. First it briefly sum m arizes im portant XML feat ures for t he init ial release of SQLServer2000 and each of t he first two Web releases. Second it provides helpful inform at ion for inst alling t he Web releases. See Chapter 12 and Chapt er 1 3 for m ore Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark. [...]... h t he sql: r elat ion at t ribut e set t ing, nam ely x m lShippers This form of an XPat h query request s t he ret urn of all t he rows fr om t he Shippers t able The XPat h query is equivalent t o SELECT * FROM Shippers in T- SQL /xmlShippers < /sql: xpath-query>... ves as a param et er ized view! Please purchase PDF Split-Merge on www.verypdf.com to remove this watermark /xmlShippers[@ShipperID=3] < /sql: xpath-query> Recall t hat annot at ed schem as sim ulat e v iews in SQL Ser ver dat abases Because views oft en... he result set appears in Figur e 6- 3 The browser docum ent w indow shows t he t ransit ion from t he last few r ows for ShipperI D 1 t o t he first few rows for ShipperI D 2 /Shipper < /sql: xpath-query> Figu re 6 - 3 An e x cer pt fr om a r e su... es t he param et er as @MyI D The par am et er’s default v alue is 1 1< /sql: param> < /sql: header> SELECT * FROM Shippers WHERE ShipperID=@MyID FOR XML AUTO < /sql: query> Figur e 6- 12 shows a browser inv ok ing t he t em plat e file w it h it s result set The... file SELECT * FROM Shippers FOR XML RAW < /sql: query> Figur e 6- 11 illust rat es a URL point ing t o t he t em plat e file and t he corr esponding result set in t he browser The FOR XML clause det erm ines t he XML for m at for t he result set The br owser’s Addr ess box in Figur e 6- 11 point s t... follow ing docum ent illust rat es t he XML for m at t ing for a sim ple T- SQL st at em ent The ROOT elem ent designat es sql as r efer encing t he ur n: schem asm icr osoft - com : xm l- sql nam espace Not ice t hat you em bed a T- SQL st at em ent in a sql: quer y elem ent The T- SQL st at em ent r et r iev es all rows and colum ns from t he Shippers dat a source The definit ion for t his dat a source... source, it doesn’t requir e a sql: field at t ribut e set t ing t o link it t o a colum n wit hin t he row source . Shippers in T- SQL. <ROOT xmlns :sql= "urn:schemas -microsoft- com:xml -sql& quot;> < !-- xmlShippersSchemaT.xml -- & gt; < ;sql: xpath-query mapping-schema="xmlShippersSchema.xml">. xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns :sql= "urn:schemas -microsoft- com:mapping-schema"> < !-- xmlShippersSchema.xml -- & gt; <xsd:element name="xmlShippers” sql: relation="Shippers”