Upload
andera4u
View
215
Download
0
Embed Size (px)
Citation preview
8/13/2019 sql2005_xml_forxml
1/16
2007 Microsoft Corporation. Allrights reserved.
Microsoft SQL Server 9.0 Technical Articles
What's New in FOR XML in Microsoft SQL Server 2005
Michael RsMicrosoft Corporation
!"ne 200#"pdated !"ne 200$
Applies to% Microsoft SQL Server 200$
Summary:This first in a series of articles & Michael Rs details the 'a(or ne) capa&ilities of theserver*side +,R -ML cla"se in the net version of SQL Server. These ena&le o" to f"rther enhance
the -ML s"pport in o"r applications and to )rite eas*to*'aintain relational data*to*-MLaggregations. /2 printed pages1
Contents
ntrod"ction3ac4)ards*Co'pati&ilit )ith SQL Server 2000ntegration )ith the -ML 5ata Tpe
Assigning +,R -ML Res"lts6esting of +,R -ML pressions
The 6e) 8AT ModeAdding -ML 6a'espaces
Rec"rsion and +,R -MLMore +,R -ML tensions
Concl"sion
Introduction
n Microsoft SQL Server 2000: )e introd"ced the +,R -ML cla"se to the SELECTstate'ent. Thiscla"se provides the a&ilit to aggregate the relational ro)set ret"rned & the SELECTstate'entinto -ML. +,R -ML on the server s"pports three 'odes;RA
8/13/2019 sql2005_xml_forxml
2/16
'"lti*tho"sand line -8LCT 'ode >"eries. also: ho)ever: "nderstand the "sa&ilit:'aintaina&ilit: and co'pleit iss"es.
n SQL Server 200$: )e no) have alternatives to the ?>"er fro' hell.? n the follo)ing: )e )illta4e a loo4 at the 'ain feat"res added to +,R -ML in SQL Server 200$% ntegration )ith the -MLdata tpe: assigna&ilit and nesting of epressions: and the ne) 8AT 'ode. "eries can &e 'ore easil
)ritten "sing the ne) feat"res.
3esides these ne) feat"res: +,R -ML also provides a co"ple of additional ne): s'aller feat"res that
are s"''ari@ed at the end of this section and s"pports an easier )a to generate na'espace*a)are doc"'ents.
Note +,R -ML contin"es to &e a ro)set aggregation cla"se of the SQL SLCT state'ent: andth"s cannot transfor' side*effect o"tp"t fro' stored proced"res. f o" )ant res"lts of so'e for'of processing transfor'ed into -ML: please "se either a "ser*defined f"nction or a vie).
Backwards-Compatibility with SQL Serer !"""
An i'portant aspect of +,R -ML in SQL Server 200$ is the &ac4)ards*co'pati&ilit )ith +,R -ML in
SQL Server 2000. The &ac4)ards*co'pati&ilit aspects can &e split into the follo)ing categories%
&"g fies: co'pati&ilit 'ode changes: and preserving +,R -ML &ehavior.
n the first categor: &"g fies: )e incorporated so'e &"g fies )ith respect of treat'ent of vie)s
and s"&>"eries in the A=T, 'ode. "er in
SQL Server 200$ )ill ret"rn its res"lt as an instance of tpe narchar%ma$&. More precisel: it )illret"rn its res"lt as a single ro): single col"'n ro)set )here the cell contains the string val"e oftpe narchar%ma$&.
Inte'ration with the ()L *ata Type
8/13/2019 sql2005_xml_forxml
3/16
ret"rns the Customersele'ents as an ()Ldata tpe instance: instead of the narchar%ma$&instance that )o"ld have &een the case )itho"t the T+,Edirective.
This res"lt is g"aranteed to confor' to the )ell*for'edness constraints provided & the ()Ldatatpe. Since the res"lt is an ()L data tpe instance: o" can also "se (Queryepressions to >"erand reshape the res"lt. +or ea'ple: the follo)ing epression retrieves the Customercontact
na'e into a ne) ,ersonele'ent.
Cop Code
SELECT (SELECT * FROM Customers FOR XML AUTO, TYPE).query(
'"er.
LetBs loo4 at the ea'ple. The follo)ing +,R -ML -8LCT >"er is ret"rning Customer ele'ents:containing their orders and the e'ploees that )or4 on their orders. +or si'plification: )e are onl
ret"rning a single propert per ele'ent.
Cop Code
SELECT 6 s TA7,
8/13/2019 sql2005_xml_forxml
4/16
-ULL s Pret,
Customer51 s +Customer868Customer51+,
-ULL s +Orer898Orer51+,
-ULL s +Em:0oyee8;8Lst-me+
FROM Customers
U-5O- ALL
SELECT 9,
6,
Customers.Customer51,
Orers.Orer51,
-ULL
FROM Orers
O5- Customers O- Orers.Customer51 Customers.Customer51
U-5O- ALL
SELECT 15ST5-CT ;,
6,
Customers.Customer51,
-ULL,
Em:0oyees.Lst-me
FROM Customers
O5- Orers O- Customers.Customer51 Orers.Customer51
O5- Em:0oyees O- Orers.Em:0oyee51 Em:0oyees.Em:0oyee51
OR1ER 3Y +Customer868Customer51+,+Em:0oyee8;8Lst-me+,+Orer898Orer51+
FOR XML EXPL5C5T
ret"rns /onl first c"sto'er sho)n1:
Cop Code
8/13/2019 sql2005_xml_forxml
5/16
"er for each of the three ?entit? ele'ents: and nest the' to epress
the hierarch. "er that eploits the nesta&ilit of +,R -ML: theA=T, 'ode: and the ne) -ML data tpe.
,ne 'a sa: ho)ever: that the previo"s >"er )as too si'ple to reflect a real )orld scenario )ith+,R -ML -8LCT.
+or ea'ple: so'e of the strengths of the eplicit 'ode are to 'i attri&"tes and ele'ents at )ill:create )rappers and nested: co'ple properties: and even create space*separated val"e lists and'ied content. 6one of these res"lts can &e achieved & nesting +,R -ML A=T, >"eries. So do )e
still have to )rite -8LCT 'ode >"eries to get these 'apping capa&ilitiesD
8/13/2019 sql2005_xml_forxml
6/16
5onBt despairE A ne) +,R -ML 'ode gives o" the sa'e flei&ilit in a '"ch si'pler )a. The ne)8AT 'ode: together )ith the a&ilit to nest +,R -ML epressions: has &eco'e the si'plest )a to
generate co'ple -ML doc"'ents.
The 8AT 'ode allo)s o" to "se an -8ath*li4e snta as a col"'n na'e: )hich then is 'appedinto an attri&"te /e.g.: 34a31: ele'ent /e.g.: 3e31: s"& ele'ent str"ct"re /3e56e!31: ele'ent
content /3731: tet node /3te$t%&31: or data val"e /3data%&31. As )ith the RA< 'ode: the defa"lt
na'e for the ro) ele'ent is rowand can &e over)ritten )ith an 6C6a'e /a na'e )itho"t aprefi1.
LetBs loo4 at so'e ea'ples. +irst: let "s give the 8AT 'ode for'"lation of the a&ove -8LCT'ode >"er.
Cop Code
SELECT Customer51 s +Customer51+,
(SELECT Orer51 s +Orer51+
FROM Orers
HIERE Orers.Customer51 Customers.Customer51
FOR XML PATI('Orer'), TYPE),
(SELECT 15ST5-CT Lst-me s +Lst-me+
FROM Em:0oyees
O5- Orers O- Orers.Em:0oyee51 Em:0oyees.Em:0oyee51
HIERE Customers.Customer51 Orers.Customer51
FOR XML PATI('Em:0oyee'), TYPE)
FROM Customers
FOR XML PATI('Customer')
This ea'ple is si'ilar to the A=T, 'ode version and ret"rns the sa'e res"lt.
6o) letBs loo4 at so'e of the 8AT 'ode specific capa&ilities. The net >"er ta4es the c"sto'erinfor'ation and gro"ps the address and contact infor'ation into separate s"& ele'ents "sing a
'ore co'ple path epression as col"'n alias: and;"sing the ne) R,,T directive;adds a rootnode aro"nd it for good 'eas"re.
Cop Code
SELECT Customer51 s +Customer51+,
Com:y-me,
Aress s +ressstreet+,
C&ty s +ress!&ty+,
Re&o s +ressre&o+,
Post0Coe s +ressJ&:+,
Coutry s +ress!outry+,
Cot!t-me s +!ot!tme+,
Cot!tT&t0e s +!ot!tt&t0e+,
PKoe s +!ot!t:Koe+,
F4 s +!ot!t$4+
8/13/2019 sql2005_xml_forxml
7/16
FROM Customers
FOR XML PATI('Customer'), ROOT('o!')
This >"er res"lts in the follo)ing doc"'ent /onl sho)ing the first c"sto'er ele'ent1.
Cop Code
;>>>B@;96;>>>B?D@D
8/13/2019 sql2005_xml_forxml
8/16
-ULL s +!ot!t8@8me8e0emet+,
-ULL s +!ot!t8@8t&t0e8e0emet+,
-ULL s +!ot!t8@8:Koe8e0emet+,
-ULL s +!ot!t8@8$48e0emet+
FROM Customers
U-5O- ALL
SELECT 9, 6,
6,
Customer51, Com:y-me,
-ULL, -ULL, -ULL, -ULL, -ULL,
-ULL, -ULL, -ULL, -ULL
FROM Customers
U-5O- ALL
SELECT ;, 9,
6,
Customer51, -ULL,
Aress, C&ty, Re&o, Post0Coe, Coutry,
-ULL, -ULL, -ULL, -ULL
FROM Customers
U-5O- ALL
SELECT @, 9,
6,
Customer51, -ULL,
-ULL, -ULL, -ULL, -ULL, -ULL,
Cot!t-me, Cot!tT&t0e, PKoe, F4
FROM Customers
OR1ER 3Y +o!868ummy8K&e+,+Customer898Customer51+
FOR XML EXPL5C5T, TYPE
6o) )e 4no) )h the -8LCT 'ode is so'eti'es called the ?>"er fro' hell.?
Last &"t not least: the follo)ing gives "s an ea'ple of generating a val"e list: and sho)s the "se
of a tet node.
Cop Code
SELECT Customer51 s +51+,
(SELECT Orer51 s +t()+
FROM Orers
HIERE Customers.Customer51Orers.Customer51
8/13/2019 sql2005_xml_forxml
9/16
FOR XML PATI('')
) s +Orer51s+,
Com:y-me,
Cot!tT&t0e s +Cot!t-meCot!tT&t0e+,
Cot!t-me s +Cot!t-mete4t()+,
Post0Coe s +AressN5P+,
Aress s +AressStreet+,
C&ty s +AressC&ty+
FROM Customers
FOR XML PATI('Customer')
This creates a res"lt of the for' /sho)ing one ea'ple c"sto'er1:
Cop Code
;BD 6>;@ 6>@6D 6>?>> 6>??>+"
8/13/2019 sql2005_xml_forxml
10/16
a voca&"lar: and to associate -ML Sche'a infor'ation /and potentiall other infor'ation1 )ith an-ML ele'ent or attri&"te.
+,R -ML: in SQL Server 2000: p"ts the &"rden of generating and 'aintaining -ML na'espacesco'pletel on the >"er )riter. The -ML na'espace declaration attri&"tes had to &e created: li4eever other attri&"te: )ith the na'espace =R &eing the col"'n val"e. =nless the generated -ML
)as in attri&"te*centric for': this 'eant that the >"er had to &e )ritten "sing the -8LCT 'ode.
+or ea'ple: the follo)ing >"er p"ts the res"lting C"sto'er ele'ents and its propert ele'entsinto the na'espace urn:e$ample8com6customer%
Cop Code
SELECT 6 s t, -ULL s :ret,
'ure4m:0e.!om!ustomer' s +!ustCustomer8684m0s!ust+,
Customer51 s +!ustCustomer868!ustCustomer518e0emet+,
Cot!t-me s +!ustCustomer868!ustCot!t-me8e0emet+
FROM Customers
FOR XML EXPL5C5T
The res"lt of the >"er is /onl first t)o ele'ents sho)n1%
Cop Code
6a'espace declarations are not reall attri&"tes in the -ML data 'odel. Th"s: the 8AT 'ode doesnot allo) the' to &e specified as attri&"tes.
n order to si'plif the "se of -ML na'espaces in +,R -ML: )e have added s"pport for the
8/13/2019 sql2005_xml_forxml
11/16
, 'ure4m:0e.!om!ustomer' s +!+
, 'ure4m:0e.!omorer' s +o+
)
SELECT Customer51 s +51+,
(SELECT Orer51 s +Orer51+
$rom Orers
Kere Customers.Customer51Orers.Customer51
FOR XML PATI('oOrer'), TYPE
) s +!Orers+,
Com:y-me s +!Com:y-me+,
Cot!tT&t0e s +!Cot!t-meCot!tT&t0e+,
Cot!t-me s +!Cot!t-mete4t()+,
Post0Coe s +!AressN5P+,
Aress s +!Aress!Street+,
C&ty s +!Aress!C&ty+
FROM Customers
FOR XML PATI('!Customer'), ROOT('o!')
As the follo)ing partial res"lt sho)s: the -ML na'espace declarations are c"rrentl added at thetop*level ele'ents of ever +,R -ML selection%
Cop Code
?9+ "
B>9+ "
8/13/2019 sql2005_xml_forxml
12/16
Orer51+6>;D+ "
D9+ "
66+ "
"er "sed the 5+A=LT cla"se for adding a defa"lt na'espace. 6ote that if there arenested -ML doc"'ents )ith no defa"lt na'espace incl"ded in the res"lt: a slight perfor'ancepenalt has to &e paid to 'a4e s"re that the preserve their a&sence of a defa"lt na'espace.
+inall: the "er for'"lation ti'e. Since )e no) can
nest +,R -ML epressions: )e can easil generate the rec"rsive hierarchies "sing "ser*definedf"nctions.
+or ea'ple: the follo)ing "ser*defined f"nction creates a nested -ML doc"'ent detailing the partslist for a specific part. +irst: let "s define so'e ea'ple data%
Cop Code
CREATE TA3LE PARTS(& &t, :ret &t, me r!Kr(D>>))
7O
5-SERT 5-TO PARTS
SELECT 6, -ULL, -'!r'
U-5O-
SELECT 9, 6, -'e&e'
U-5O-
8/13/2019 sql2005_xml_forxml
13/16
SELECT ;, 6, -'oy'
U-5O-
SELECT @, ;, -'oor'
U-5O-
SELECT D, ;, -'$eer'
U-5O-
SELECT ?, @, -'&o'
U-5O-
SELECT B, 9, -':&sto'
6et: )e define the f"nction that for a given part n"'&er ret"rns the s"&parts in -ML for'%
Cop Code
CREATE FU-CT5O- PrtsL&st(Prts-o &t)
RETUR-S XML
H5TI RETUR-S -ULL O- -ULL 5-PUT
3E75- RETUR-
(SELECT & s +&+, me s +me+,
CASE HIE- :retPrts-o
TIE- o.PrtsL&st(&)
E-1
FROM o.PARTS HIERE :retPrts-o
FOR XML PATI('Prts'), TYPE)
E-1
"er eec"tion does not calc"late the f"nctionrec"rsivel if the opti'i@er decides to appl the filter after eec"ting the selection. ec"ting thefollo)ing epression )ill ret"rn part G )ith it s"&parts.
Cop Code
se0e!t & s +&+, me s +me+,
CASE HIE- &;
TIE- o.PrtsL&st(&)
E-1
FROM PARTS
HIERE &;
FOR XML PATI('Prts'), TYPE
Ret"rns%
Cop Code
8/13/2019 sql2005_xml_forxml
14/16
8/13/2019 sql2005_xml_forxml
15/16
2. The ELE)ENTSdirective provides an -S6L option to 'ap N9LLval"es to an ele'ent
)ith an attri&"te $si:nil3true3. +or ea'ple:
Cop Code
SELECT *
FROM Customers
HIERE Re&o &s u00
FOR XML PATI('Customer'), ELEME-TS XS5-5L
ret"rns /onl sho)ing the first c"sto'er1:
Cop Code
>6XMLS!Kem&st!e+"
>>B@;96;>>>B?D@D
8/13/2019 sql2005_xml_forxml
16/16
Cop Code
>6XMLS!Kem+
4m0ssq0ty:es
+Ktt:s!Kems.m&!roso$t.!omsq0serer9>>@sq0ty:es+
e0emetForm1e$u0t+qu0&$&e+"
>@sq0ty:es+ "
"eries and the "eries in a si'pler: 'ore 'aintaina&le )a. The a&ilit to nest +,R
-ML >"eries also provides a 'echanis' to generate rec"rsive hierarchies.
There )ill still &e a fe) cases )here o" 'a )ant to "se the -8LCT 'ode /in order to generateC5ATA sections or to "se the #$mlte$tdirective: for ea'ple1: &"t the ne) f"nctionalit sho"ld
'a4e the ?>"er fro' hell? a '"ch less fre>"ent enco"nter.