SlideShare ist ein Scribd-Unternehmen logo
1 von 57
Downloaden Sie, um offline zu lesen
XML on SQL Server
Kristian Torp
Department of Computer Science
Aalborg University
people.cs.aau.dk/˜torp
torp@cs.aau.dk
November 5, 2015
daisy.aau.dk
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 1 / 69
Outline
1 Create and Insert
2 XML Queries
Saying Hello
exist()
query()
value()
nodes()
3 Generating XML from Select Statements
Raw
Auto
Path
4 Summary
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 2 / 69
Learning Goals
Goals
Overview of XML support on SQL Server
Storing XML directly SQL Server
Querying XML on SQL Server
Standard
Concepts are (fairly) general
Code is DBMS specific
DBMS Versions Tested
SQL Server 2008
SQL Server 2012
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 3 / 69
Create the Relational Schema
Example (Create the Table)
create table course (
id i n t primary key ,
dsc xml not n u l l )
Note
A create table statement as we know it
The XML data type named xml
A mixture of “old” data types int and new data types xml
The not-null constraint, just because we love them!
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 5 / 69
Insert Data
Example (Insert a Row)
i n s e r t i n t o course values (1 ,
’<courses>
<course id =”22” ects =”5” >
<name>XML</name>
<description >XPath and XQuery f o r querying XML documents
</ description >
</course>
<course id =”11” ects =”5” >
<name>DB</name>
<description >Querying a r e l a t i o n a l database </ description >
</course>
</courses> ’ )
Note
A plain insert statement
The XML data type coersed from the text string
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 6 / 69
Insert Additional Row
Example (Insert another Row)
i n s e r t i n t o course values (2 ,
’<courses>
<course id =”44” ects =”10”>
<name>Imperative Programming </name>
<description >C and Pointers </ description >
</course>
<course id =”55” ects =”5” >
<name>OOP</name>
<description >Working with classes and objects </ description >
</course>
</courses> ’ )
Note
Nothing new conceptually, simply an insert statement
We now have a small database!
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 7 / 69
Database Content
Example
id dsc
1
<courses>
<course id=” 22 ” ects=” 5 ”>
<name>XML</name>
<description>XPath . . . </ description>
</ course>
<course id=” 11 ” ects=” 5 ”>
<name>DB</name>
<description>Querying . . . </ description>
</ course>
</ courses>
2
<courses>
<course id=” 44 ” ects=” 10 ”>
<name>Imperative Programming</name>
<description>C . . . </ description>
</ course>
<course id=” 55 ” ects=” 5 ”>
<name>OOP</name>
<description>Working . . . </ description>
</ course>
</ courses>
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 8 / 69
Not Well-formed XML
Example (Insert a Row)
i n s e r t i n t o course values (3 ,
’<course id =”77” ects =”10”>
<name>Object−Oriented Analysis and Design </name>
<description >Modeling with classes and objects </ description >
</course>
<course id =”88” ects =”5” >
<name>Software Architecture </name>
<description >The big picture </ description >
</course> ’ )
Note
Has no root element
This works fine!
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 9 / 69
Not Well-formed XML, cont.
Example (End tag is missing)
i n s e r t i n t o course values (4 ,
’<course id =”99” ects =”10”>
<name>Operating Systems </name>
<description >Linux , Windows , and so on</ description > ’ )
Note
XML parsing: line 3, character 61, unexpected end of input
Example (Fix the missing end tag)
i n s e r t i n t o course values (4 ,
’<course id =”99” ects =”10”>
<name>Operating Systems </name>
<description >Linux , Windows , and so on</ description >
</course> ’ )
Note
Works!
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 10 / 69
Not Well-formed XML, cont.
Example (Nesting Wrong)
i n s e r t i n t o course values (5 ,
’<course id =”13” ects =”5” >
<name><more>Compiler Construction </name></more>
<description >Lex and Yacc</ description >
</course> ’ )
Note
XML parsing: line 2, character 41, end tag does not match start tag
Example (“Weird” Cases)
i n s e r t i n t o course values (6 , ’ ’ )
i n s e r t i n t o course values (7 , ’NULL ’ )
Note
Works!
Example (Does this work?)
i n s e r t i n t o course values (8 ,NULL)
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 11 / 69
Summary: Create and Insert
Main Points
Create table has new XML data type
Very similar to the SQL/XML XML data type
Insert statement works as expected
XML documents, without root possible, tags must be matching
Delete works as expected
Not shown here
There is special support for updating XML columns
Updating only a part of the XML document in a cell
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 12 / 69
Recall the Database Content
Example
id dsc
1
<courses>
<course id=” 22 ” ects=” 5 ”>
<name>XML</name>
<description>XPath . . . </ description>
</ course>
<course id=” 11 ” ects=” 5 ”>
<name>DB</name>
<description>Querying . . . </ description>
</ course>
</ courses>
2
<courses>
<course id=” 44 ” ects=” 10 ”>
<name>Imperative Programming</name>
<description>C . . . </ description>
</ course>
<course id=” 55 ” ects=” 5 ”>
<name>OOP</name>
<description>Working . . . </ description>
</ course>
</ courses>
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 15 / 69
Say Hello
Example (Use the Course Table)
select dsc . query ( ” l e t $s := ’ Hello , World ! ’ return $s ” )
from course
Note
This does not work!
And I used a long time to understand why not :-)
Example (Try Again (almost the same))
select dsc . query ( ’ l e t $s := ” Hello , World ! ” return $s ’ )
from course
Note
Works!
’ and “ are different!
dsc is a column on table course
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 16 / 69
Say Hello, cont
Example (Say Hello from XQuery)
select dsc . query ( ’ l e t $s := ” Hello , World ! ” return $s ’ )
from course
Example (Result)
(No column name)
Hello, World!
Hello, World!
Note
A hello for each row in the table
This is a simple XQuery
SQL Server generally follows the XQuery standard well
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 17 / 69
Say Hello, the T-SQL Way
Example (Use a Variable)
declare @x xml
select @x. query ( ’ l e t $s := ” Hello , World ! ” return $s ’ )
Example (Result)
(No column name)
null
Example (Initialize the Variable)
declare @x xml = ’ ’
select @x. query ( ’ l e t $s := ” Hello , World ! ” return $s ’ )
Example (Result)
(No column name)
Hello, World!
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 18 / 69
Document IDs with more than 5 ECTS Courses
Example (Use exist() in where )
select id
from course
where dsc . e x i s t ( ’ / / course [ @ects > 5] ’ )
Note
An expression of non-boolean type specified in a context where a
condition is expected, near ’)’
Example (Use exist() in select )
select id , dsc . e x i s t ( ’ / / course [ @ects > 5] ’ )
from course
Example (Result)
id (No column name)
1 0
2 1
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 20 / 69
Retrying Document IDs with more than 5 ECTS Courses
Example (Use exist() in where )
select id
from course
where dsc . e x i s t ( ’ / / course [ @ects > 5] ’ ) = 1
Example (Result)
id
2
Note
exist() returns 0, 1 or null
This is a surprise! (when used to other DBMSs)
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 21 / 69
Recall the Database Content
Example
id dsc
1
<courses>
<course id=” 22 ” ects=” 5 ”>
<name>XML</name>
<description>XPath . . . </ description>
</ course>
<course id=” 11 ” ects=” 5 ”>
<name>DB</name>
<description>Querying . . . </ description>
</ course>
</ courses>
2
<courses>
<course id=” 44 ” ects=” 10 ”>
<name>Imperative Programming</name>
<description>C . . . </ description>
</ course>
<course id=” 55 ” ects=” 5 ”>
<name>OOP</name>
<description>Working . . . </ description>
</ course>
</ courses>
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 23 / 69
XPath for finding Course Names
Example (Use XPath)
select dsc . query ( ’ / courses / course /name ’ ) as res
from course
Example (Result)
res
<name>XML</name>
<name>DB</name>
<name>Imperative Programming</name>
<name>OOP</name>
null
Note
What was expected, it is really XPath!
A null value for each row where the XPath has no match
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 24 / 69
XPath for finding Course Names in Text Format
Example (Use text())
select dsc . query ( ’ / courses / course /name/ t e x t ( ) ’ ) as res
from course
Example (Result)
res
XMLDB
Imperative ProgrammingOOP
Note
What was expected!
Not really useful in an SQL context
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 25 / 69
Accessing Attributes
Example (Look at Attributes)
select dsc . query ( ’ / courses / course / @ects ’ ) as res
from course
Note
XQuery [course.dsc.query()]: Attribute may not appear outside of an
element
Unexpected!
Example (Look at Attributes)
select dsc . query ( ’sum ( / courses / course / @ects ) ’ ) as res
from course
Example (Result)
res
10
15
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 26 / 69
More XPath
Example (Find 5 ECTS Courses)
select dsc . query ( ’ / courses / course [ @ects =5]/name/ t e x t ( ) ’ ) as res
from course
Example (Result)
res
XMLDB
OOP
Example (Filtering using where )
select dsc . query ( ’ / / course /name/ t e x t ( ) ’ ) as res
from course
where id = 1
Example (Result)
res
XMLDB
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 27 / 69
Multi-Column XPath
Example (More than One Column)
select dsc . query ( ’ / / course /name ’ ) as name,
dsc . query ( ’ / / course / description ’ ) as descr
from course
Example (Result)
name descr
<name>XML</name>
<name>DB</name>
<description>XPath . . . </ description>
<description>Querying . . . </ description>
<name>Imperative . . . </name>
<name>OOP</name>
<description>C and Pointers</ description>
<description>Working . . . </ description>
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 28 / 69
Multi-Column XPath with text()
Example (Multiple Columns and text())
select dsc . query ( ’ / / course /name/ t e x t ( ) ’ ) as name,
dsc . query ( ’ / / course / description / t e x t ( ) ’ ) as descr
from course
Example (Result)
name descr
XMLDB XPath ... Querying ...
Imperative Programming OOP C and Pointers Working ...
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 29 / 69
Full FLWOR
Example (FLWOR Query)
select dsc . query ( ’ f o r $c in / / course
where $c / @ects >= 5
return ( $c /name, $c / description ) ’ ) as res
from course
Example (Result)
res
<name>XML</name>
<description>XPath and XQuery f o r querying XML documents</ description>
<name>DB</name>
<description>Querying a r e l a t i o n a l database</ description>
<name>Imperative Programming</name>
<description>C and Pointers</ description>
<name>OOP</name>
<description>Working with classes and objects</ description>
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 30 / 69
Recall the Database Content
Example
id dsc
1
<courses>
<course id=” 22 ” ects=” 5 ”>
<name>XML</name>
<description>XPath . . . </ description>
</ course>
<course id=” 11 ” ects=” 5 ”>
<name>DB</name>
<description>Querying . . . </ description>
</ course>
</ courses>
2
<courses>
<course id=” 44 ” ects=” 10 ”>
<name>Imperative Programming</name>
<description>C . . . </ description>
</ course>
<course id=” 55 ” ects=” 5 ”>
<name>OOP</name>
<description>Working . . . </ description>
</ course>
</ courses>
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 32 / 69
Get Course Names in a Relational Fashion
Example (Using the value() Function)
select dsc . value ( ’ / / course /name ’ , ’ nvarchar (30) ’ )
from course
Note
XQuery [course.dsc.value()]: ’value()’ requires a singleton (or empty
sequence), found operand of type ’xdt:untypedAtomic *’
Example (Singleton’ing)
select dsc . value ( ’ ( / / course /name ) [ 1 ] ’ , ’ nvarchar (30) ’ )
from course
Example (Result)
(No column name)
XML
Imperative Programming
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 33 / 69
Retrying Get Course Names in a Relational Fashion
Example (First)
select dsc . value ( ’ ( / / course [ 1 ] / name ) [ 1 ] ’ , ’ nvarchar (30) ’ )
from course
Example (Result)
(No column name)
XML
Imperative Programming
Example (Last)
select dsc . value ( ’ ( / / course /name ) [ l a s t ( ) ] ’ , ’ nvarchar (30) ’ )
from course
Example (Result)
(No column name)
DB
OOP
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 34 / 69
Value and Wrong SQL Data Type
Example (First)
select dsc . value ( ’ ( / / course /name ) [ 1 ] ’ , ’ nvarchar (30) ’ )
from course
Example (Result)
(No column name)
XML
Imperative Programming
Example (Last)
select dsc . value ( ’ ( / / course /name ) [ 1 ] ’ , ’ nvarchar (3) ’ )
from course
Example (Result)
(No column name)
XML
Imp
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 35 / 69
value() and Wrong SQL Data Type, cont.
Example (String as an Integer)
select dsc . value ( ’ ( / / course /name ) [ 1 ] ’ , ’ i n t ’ )
from course
Note
Conversion failed when converting the nvarchar value ’XML’ to data
type int.
Example (Integer Attribute)
select dsc . value ( ’ ( / / course / @ects ) [ 1 ] ’ , ’ i n t ’ )
from course
Example (Result)
(No column name)
5
10
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 36 / 69
Flattening using value()
Example (String as an Integer)
select id ,
dsc . value ( ’ ( / / course / @ects ) [ 1 ] ’ , ’ i n t ’ ) as ects
from course
where dsc . e x i s t ( ’ / / course [ @ects > 5] ’ ) = 1
Example (Result)
id ects
2 10
Note
Using value() and exist() functions in same query
Still too few rows
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 37 / 69
Flattening using value(), cont
Example (String as an Integer)
select dsc . value ( ’ ( / / course / @id ) [ l a s t ( ) ] ’ , ’ i n t ’ ) as cid ,
dsc . value ( ’ ( / / course /name ) [ l a s t ( ) ] ’ , ’ nvarchar (30) ’ ) as cname ,
dsc . value ( ’ ( / / course / description ) [ l a s t ( ) ] ’ , ’ nvarchar (50) ’ ) as
dsc . value ( ’ ( / / course / @ects ) [ l a s t ( ) ] ’ , ’ i n t ’ ) as ects
from course
Example (Result)
cid cname dsc ects
11 DB Querying... 5
55 OOP Working... 5
Note
Pure relational world result
Result only has two rows (should have four)
value() cannot be used must use nodes()
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 38 / 69
Flattening using nodes()
Example (Flatten XML)
select f l a t . course . value ( ’ ( . / @id ) [ 1 ] ’ , ’ i n t ’ ) as cid ,
f l a t . course . value ( ’ ( . / name ) [ 1 ] ’ , ’ varchar (50) ’ ) as cname ,
f l a t . course . value ( ’ ( . / description ) [ 1 ] ’ , ’ varchar (50) ’ ) as dsc ,
f l a t . course . value ( ’ ( . / @ects ) [ 1 ] ’ , ’ i n t ’ ) as ects
from course
cross apply dsc . nodes ( ’ / / course ’ ) f l a t ( course )
order by cid
Example (Result)
cid cname dsc ects
11 DB Querying... 5
22 XML XPath... 5
44 Imp. C and ... 10
55 OOP Working... 5
Note
Relational view on XML data!!!
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 40 / 69
Hide XML
Example (Create a View on XML Data)
create view coursexml flat as
select f l a t . course . value ( ’ ( . / @id ) [ 1 ] ’ , ’ i n t ’ ) as cid ,
f l a t . course . value ( ’ ( . / name ) [ 1 ] ’ , ’ varchar (50) ’ ) as cname ,
f l a t . course . value ( ’ ( . / description ) [ 1 ] ’ , ’ varchar (50) ’ ) as dsc ,
f l a t . course . value ( ’ ( . / @ects ) [ 1 ] ’ , ’ i n t ’ ) as ects
from course
cross apply dsc . nodes ( ’ / / course ’ ) f l a t ( course )
Example (Query the View)
select ∗
from coursexml flat
where ects = 5 and cid > 20
order by cid
Example (Result)
cid cname dsc ects
22 XML XPath... 5
55 OOP Working... 5
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 41 / 69
Summary: Queries
Functions
Function Descrition
exist() Check for existence
query() XPath and XQuery
value() Extract single value
nodes() For flatting nodes
modify() For (fine grained) updates of XML
Note
SQL Server only
SQL/XML via programming languages
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 42 / 69
Master-Detail Example I
Order
id
customer name
amount
OrderLine
lineno
price per unit
quan. desc.
has
Assumptions
An order cannot exist without order lines
Amount is the sum of the amount of order lines for a give order
Must be larger than zero
Derived from order lines but stored because often queried
Order line is a weak entity
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 44 / 69
Master-Detail Example II
Example
create table oorder (
oid i n t primary key ,
cname varchar (30) not n u l l )
Example
create table oorder line (
oid i n t not null ,
line no i n t not n u l l check ( line no > 0) ,
dsc varchar (50) not null ,
quantity i n t not n u l l check ( quantity > 0) ,
price each numeric (6 ,2) not n u l l check ( price each > 0.0) ,
c o n s t r a i n t ol pk primary key ( oid , line no ) ,
c o n s t r a i n t o l o f k foreign key ( oid )
references oorder ( oid ) )
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 45 / 69
Get XML Out
Example
select ∗
from oorder
f o r xml raw
Example (Result)
<row oid=” 11 ” cname=”Anna” />
<row oid=” 22 ” cname=” Benny ” />
<row oid=” 33 ” cname=” Curt ” />
Note
A select statement
The for xml raw is SQL Server specific
Pretty ugly format ;-)
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 47 / 69
Get Elements Out
Example
select ∗
from oorder
f o r xml raw , elements
Example (Result)
<row>
<oid>11</ oid>
<cname>Anna</ cname>
</ row>
<row>
<oid>22</ oid>
<cname>Benny</ cname>
</ row>
<row>
<oid>33</ oid>
<cname>Curt</ cname>
</ row>
Note
Much nicer
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 48 / 69
Rename Element Names
Example
select oid as ” order −id ” ,
cname as ” customer−name”
from oorder
f o r xml raw , elements
Example (Result)
<row>
<order −id>11</ order −id>
<customer−name>Anna</ customer−name>
</ row>
<row>
<order −id>22</ order −id>
<customer−name>Benny</ customer−name>
</ row>
snip
Note
A select statement
The for xml raw is SQL Server specific
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 49 / 69
Rename Enclosed Element
Example
select oid as ” order −id ” ,
cname as ” customer−name”
from oorder
f o r xml raw ( ’ customer ’ ) , elements
Example (Result)
<customer>
<order −id>11</ order −id>
<customer−name>Anna</ customer−name>
</ customer>
<customer>
<order −id>33</ order −id>
<customer−name>Curt</ customer−name>
</ customer>
Note
All columns are elements
Missing a root element
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 50 / 69
Adding a Root Element
Example
select oid as ” order −id ” ,
cname as ” customer−name”
from oorder
f o r xml raw ( ’ customer ’ ) , elements , root ( ’ customers ’ )
Example (Result)
<customers>
<customer>
<order −id>11</ order −id>
<customer−name>Anna</ customer−name>
</ customer>
<customer>
<order −id>33</ order −id>
<customer−name>Curt</ customer−name>
</ customer>
</ customers>
Note
Now nice XML document
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 51 / 69
Joining Tables
Example
select ∗
from oorder , oorder line
where oorder . oid = oorder line . oid
f o r xml raw , elements
Example (Result (snip))
<row>
<oid>11</ oid>
<cname>Anna</ cname>
<oid>11</ oid>
<line no>1</ line no>
<dsc>Cola</ dsc>
<quantity>1</ quantity>
<price each>10.00</ price each>
</ row>
Note
No nesting
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 52 / 69
Summary: Raw
Overview
Fast to get XML fragments out
Nice simple extension to select statement
Default output in attributes
Either all attributes or all elements
Mixture of attributes and elements hard
Is a bit “raw”
Additional options, type, xmldata, xmlschema
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 53 / 69
Get XML Out
Example
select ∗
from oorder
f o r xml auto
Example (Result)
<row oid=” 11 ” cname=”Anna” />
<row oid=” 22 ” cname=” Benny ” />
<row oid=” 33 ” cname=” Curt ” />
Note
At first glanse looks like for xml raw
But wait, there is more ...
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 55 / 69
The Power of Auto
Example
select ∗
from oorder , oorder line
where oorder . oid = oorder line . oid
and oid < 30
f o r xml auto
Example (Result)
<oorder oid=” 11 ” cname=”Anna”>
<o order line oid=” 11 ” line no =” 1 ” dsc=” Cola ” quantity =” 1 ” price each=” 10.00 ”
<o order line oid=” 11 ” line no =” 2 ” dsc=” Chips ” quantity =” 1 ” price each=” 15.00
</ oorder>
<oorder oid=” 22 ” cname=” Benny ”>
<o order line oid=” 22 ” line no =” 1 ” dsc=” Cola ” quantity =” 1 ” price each=” 10.00 ”
<o order line oid=” 22 ” line no =” 2 ” dsc=” Burger ” quantity =” 2 ” price each=” 20.0
<o order line oid=” 22 ” line no =” 3 ” dsc=” Chips ” quantity =” 1 ” price each=” 15.00
</ oorder>
Note
Does automatic nesting at “natural” levels
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 56 / 69
Rename Elements
Example
select oorder . oid as ” order ” ,
oorder . cname as ” customer ” ,
oorder line . dsc as ” description ” ,
oorder line . quantity ,
oorder line . price each as ” price ”
from oorder , oorder line
where oorder . oid = oorder line . oid
f o r xml auto
Example (Result (snip))
<oorder order=” 11 ” customer=”Anna”>
<o order line description =” Cola ” quantity =” 1 ” price=” 10.00 ” />
<o order line description =” Chips ” quantity =” 1 ” price=” 15.00 ” />
</ oorder>
<oorder order=” 22 ” customer=” Benny ”>
<o order line description =” Cola ” quantity =” 1 ” price=” 10.00 ” />
<o order line description =” Burger ” quantity =” 2 ” price=” 20.00 ” />
<o order line description =” Chips ” quantity =” 1 ” price=” 15.00 ” />
</ oorder>
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 57 / 69
Add Root Element + Rename
Example
select ” order ” . oid as ” id ” ,
” order ” . cname as ” customer ” ,
” l i n e ” . dsc as ” description ” ,
” l i n e ” . quantity ,
” l i n e ” . price each as ” price ”
from oorder as ” order ” , oorder line as ” l i n e ”
where ” order ” . oid = ” l i n e ” . oid
f o r xml auto , root ( ’ orders ’ )
Example (Result (snip))
<orders>
<order id=” 11 ” customer=”Anna”>
<l i n e description =” Cola ” quantity =” 1 ” price=” 10.00 ” />
<l i n e description =” Chips ” quantity =” 1 ” price=” 15.00 ” />
</ order>
<order id=” 22 ” customer=” Benny ”>
<l i n e description =” Cola ” quantity =” 1 ” price=” 10.00 ” />
<l i n e description =” Burger ” quantity =” 2 ” price=” 20.00 ” />
<l i n e description =” Chips ” quantity =” 1 ” price=” 15.00 ” />
</ order>
</ orders>
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 58 / 69
Make it All Elements
Example
select ” order ” . oid as ” id ” ,
” order ” . cname as ” customer ” ,
” l i n e ” . dsc as ” description ” ,
” l i n e ” . quantity ,
” l i n e ” . price each as ” price ”
from oorder as ” order ” , oorder line as ” l i n e ”
where ” order ” . oid = ” l i n e ” . oid
f o r xml auto , elements , root ( ’ orders ’ )
Example (Result (snip))
<orders>
<order>
<id>11</ id>
<customer>Anna</ customer>
<l i n e>
<description>Cola</ description>
<quantity>1</ quantity>
<price>10.00</ price>
</ l i n e>
snip
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 59 / 69
Summary: Auto
Overview
Fast to get XML out even for joins
Row tag can only be renamed by table alias
Okay at getting nesting correct
Either attributes or elements
Options similar as for the xml raw output
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 60 / 69
Get XML Out
Example
select oorder . oid as ” id ” ,
oorder . cname as ” customer ” ,
oorder line . line no as ” lineno ” ,
oorder line . dsc as ” description ” ,
oorder line . quantity ,
oorder line . price each as ” price ”
from oorder , oorder line
where oorder . oid = oorder line . oid
f o r xml path
Example (Result (snip))
<row>
<id>11</ id>
<customer>Anna</ customer>
<lineno>1</ lineno>
<description>Cola</ description>
<quantity>1</ quantity>
<price>10.00</ price>
</ row>
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 62 / 69
Adding Structure
Example
select oorder . oid as ” id ” ,
oorder . cname as ” customer ” ,
oorder line . line no as ” lineno ” ,
oorder line . dsc as ” description ” ,
oorder line . quantity ,
oorder line . price each as ” price ”
from oorder , oorder line
where oorder . oid = oorder line . oid
f o r xml path ( ’ order ’ ) , root ( ’ orders ’ )
Example (Result (snip))
<orders>
<order>
<id>11</ id>
<customer>Anna</ customer>
<lineno>1</ lineno>
<description>Cola</ description>
<quantity>1</ quantity>
<price>10.00</ price>
</ order>
<order>
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 63 / 69
Adding Attributes and Nesting Line Items
Example
select oorder . oid as ”@id” ,
oorder . cname as ” customer ” ,
oorder line . line no as ” l i n e / lineno ” ,
oorder line . dsc as ” l i n e / description ” ,
oorder line . quantity as ” l i n e / quantity ” ,
oorder line . price each as ” l i n e / price ”
from oorder , oorder line
where oorder . oid = oorder line . oid
f o r xml path ( ’ order ’ ) , root ( ’ orders ’ )
Example (Result (snip))
<orders>
<order id=” 11 ”>
<customer>Anna</ customer>
<l i n e>
<lineno>1</ lineno>
<description>Cola</ description>
<quantity>1</ quantity>
<price>10.00</ price>
</ l i n e>
</ order>
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 64 / 69
Nesting Correctly
Example
select oorder . oid as ”@id” ,
oorder . cname as ” customer ” ,
( select oorder line . line no as ” @lineno ” ,
oorder line . dsc as ” description ” ,
oorder line . quantity as ” quantity ” ,
oorder line . price each as ” price ”
from oorder line
where oorder . oid = oorder line . oid
f o r xml path ( ’ l i n e ’ ) , type )
from oorder
f o r xml path ( ’ order ’ ) , root ( ’ orders ’ )
Example (Result (snip))
<orders>
<order id=” 11 ”>
<customer>Anna</ customer>
<l i n e lineno=” 1 ”>
<description>Cola</ description>
<quantity>1</ quantity>
<price>10.00</ price>
</ l i n e>
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 65 / 69
Summary: Auto
Overview
More complicated than raw and auto mode
Nesting “correctly” by subqueries
Cast to XML type otherwise a string!
Relies on naming conventions for attributes versus elements
Options similar as for the raw and auto modes
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 66 / 69
Summary
Summary
XPath follows the standard
XQuery follows the standard
XML support on SQL Server far from standard!
Not Looked At (RTFM :-))
Indexing XML columns
Updates
Querying XML from C#
SQLXML (Microsoft specific)
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 68 / 69
Additional Information
Web Sites
Introduction to XQuery in SQL Server 2005
Good overview, cannot find a newer
msdn.microsoft.com/en-us/library/ms345122(v=sql.90).aspx
XML Options in Microsoft SQL Server 2005
More on the XML data type, cannot find a newer
msdn.microsoft.com/en-us/library/ms345110%28v=sql.90%29.
aspx
SQLXML 4.0 Programming Concepts
Good tutorial
Must be installed separately on SQL Server 2008 and above
msdn.microsoft.com/en-us/library/ms171779.aspx
FOR XML (SQL Server)
SQL Server 2012
http://msdn.microsoft.com/en-us/library/ms178107.aspx
Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 69 / 69

Weitere ähnliche Inhalte

Was ist angesagt?

Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
Kumar
 

Was ist angesagt? (20)

Introductionto xslt
Introductionto xsltIntroductionto xslt
Introductionto xslt
 
XML and XPath details
XML and XPath detailsXML and XPath details
XML and XPath details
 
Xpath
XpathXpath
Xpath
 
X FILES
X FILESX FILES
X FILES
 
Session 4
Session 4Session 4
Session 4
 
XPATH
XPATHXPATH
XPATH
 
XPath
XPathXPath
XPath
 
Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics Collections - Sorting, Comparing Basics
Collections - Sorting, Comparing Basics
 
10. Introduction to Datastructure
10. Introduction to Datastructure10. Introduction to Datastructure
10. Introduction to Datastructure
 
XSLT presentation
XSLT presentationXSLT presentation
XSLT presentation
 
XML/XSLT
XML/XSLTXML/XSLT
XML/XSLT
 
transforming xml using xsl and xslt
transforming xml using xsl and xslttransforming xml using xsl and xslt
transforming xml using xsl and xslt
 
Day2 xslt x_path_xquery
Day2 xslt x_path_xqueryDay2 xslt x_path_xquery
Day2 xslt x_path_xquery
 
Xpath
Xpath Xpath
Xpath
 
Xslt tutorial
Xslt tutorialXslt tutorial
Xslt tutorial
 
XPath Introduction
XPath IntroductionXPath Introduction
XPath Introduction
 
XPath XSLT Workshop - Concept Listing
XPath XSLT Workshop - Concept ListingXPath XSLT Workshop - Concept Listing
XPath XSLT Workshop - Concept Listing
 
SPARQL Query Containment with ShEx Constraints
SPARQL Query Containment with ShEx ConstraintsSPARQL Query Containment with ShEx Constraints
SPARQL Query Containment with ShEx Constraints
 
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)
Wordpress (class,property,visibility,const,destr,inheritence,mysql etc)
 
Core Java Concepts
Core Java ConceptsCore Java Concepts
Core Java Concepts
 

Andere mochten auch

Sql Server execution plans
Sql Server execution plansSql Server execution plans
Sql Server execution plans
Florin Cardasim
 
7a advanced tsql
7a   advanced tsql7a   advanced tsql
7a advanced tsql
Nauman R
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-Presentation
Chuck Walker
 

Andere mochten auch (16)

D drops
D drops D drops
D drops
 
Sql Server execution plans
Sql Server execution plansSql Server execution plans
Sql Server execution plans
 
7a advanced tsql
7a   advanced tsql7a   advanced tsql
7a advanced tsql
 
SQL Functions
SQL FunctionsSQL Functions
SQL Functions
 
Sql operators & functions 3
Sql operators & functions 3Sql operators & functions 3
Sql operators & functions 3
 
Sql query analyzer & maintenance
Sql query analyzer & maintenanceSql query analyzer & maintenance
Sql query analyzer & maintenance
 
SQL Server - Querying and Managing XML Data
SQL Server - Querying and Managing XML DataSQL Server - Querying and Managing XML Data
SQL Server - Querying and Managing XML Data
 
Stored-Procedures-Presentation
Stored-Procedures-PresentationStored-Procedures-Presentation
Stored-Procedures-Presentation
 
XML Databases
XML DatabasesXML Databases
XML Databases
 
CSS3 and Responsive Web Design - Web Technologies (1019888BNR)
CSS3 and Responsive Web Design - Web Technologies (1019888BNR)CSS3 and Responsive Web Design - Web Technologies (1019888BNR)
CSS3 and Responsive Web Design - Web Technologies (1019888BNR)
 
Course Review - Lecture 12 - Next Generation User Interfaces (4018166FNR)
Course Review - Lecture 12 - Next Generation User Interfaces (4018166FNR)Course Review - Lecture 12 - Next Generation User Interfaces (4018166FNR)
Course Review - Lecture 12 - Next Generation User Interfaces (4018166FNR)
 
T-SQL Data Types (Quick Overview)
T-SQL Data Types (Quick Overview)T-SQL Data Types (Quick Overview)
T-SQL Data Types (Quick Overview)
 
Getting Started with HTML5 in Tech Com (STC 2012)
Getting Started with HTML5 in Tech Com (STC 2012)Getting Started with HTML5 in Tech Com (STC 2012)
Getting Started with HTML5 in Tech Com (STC 2012)
 
Introduction to XML and Databases
Introduction to XML and DatabasesIntroduction to XML and Databases
Introduction to XML and Databases
 
Sql hints
Sql hintsSql hints
Sql hints
 
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
Structured Query Language (SQL) - Lecture 5 - Introduction to Databases (1007...
 

Ähnlich wie XML on SQL Server

Powershell Training
Powershell TrainingPowershell Training
Powershell Training
Fahad Noaman
 
SQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQLSQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQL
Jerry Yang
 
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
Marco Gralike
 

Ähnlich wie XML on SQL Server (20)

Introduction to DTD
Introduction to DTDIntroduction to DTD
Introduction to DTD
 
Querying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptxQuerying_with_T-SQL_-_01.pptx
Querying_with_T-SQL_-_01.pptx
 
Real World Experience With Oracle Xml Database 11g An Oracle Ace’s Perspectiv...
Real World Experience With Oracle Xml Database 11g An Oracle Ace’s Perspectiv...Real World Experience With Oracle Xml Database 11g An Oracle Ace’s Perspectiv...
Real World Experience With Oracle Xml Database 11g An Oracle Ace’s Perspectiv...
 
Sql Summit Clr, Service Broker And Xml
Sql Summit   Clr, Service Broker And XmlSql Summit   Clr, Service Broker And Xml
Sql Summit Clr, Service Broker And Xml
 
PostgreSQL and XML
PostgreSQL and XMLPostgreSQL and XML
PostgreSQL and XML
 
Database management system chapter5
Database management system chapter5Database management system chapter5
Database management system chapter5
 
Ot performance webinar
Ot performance webinarOt performance webinar
Ot performance webinar
 
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of TonguesChoose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
Choose'10: Ralf Laemmel - Dealing Confortably with the Confusion of Tongues
 
Decoding and developing the online finding aid
Decoding and developing the online finding aidDecoding and developing the online finding aid
Decoding and developing the online finding aid
 
Oracle Fundamental and PL-SQL.docx
Oracle Fundamental and PL-SQL.docxOracle Fundamental and PL-SQL.docx
Oracle Fundamental and PL-SQL.docx
 
Sql tutorial
Sql tutorialSql tutorial
Sql tutorial
 
Sedna XML Database: Query Parser & Optimizing Rewriter
Sedna XML Database: Query Parser & Optimizing RewriterSedna XML Database: Query Parser & Optimizing Rewriter
Sedna XML Database: Query Parser & Optimizing Rewriter
 
215 oodb
215 oodb215 oodb
215 oodb
 
Powershell Training
Powershell TrainingPowershell Training
Powershell Training
 
Erik_van_Roon.pdf
Erik_van_Roon.pdfErik_van_Roon.pdf
Erik_van_Roon.pdf
 
SQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQLSQL Server 2000 Research Series - Transact SQL
SQL Server 2000 Research Series - Transact SQL
 
Preface
PrefacePreface
Preface
 
10053 otw
10053 otw10053 otw
10053 otw
 
NUS Hackers Club Mar 21 - Whats New in JavaSE 8?
NUS Hackers Club Mar 21 - Whats New in JavaSE 8?NUS Hackers Club Mar 21 - Whats New in JavaSE 8?
NUS Hackers Club Mar 21 - Whats New in JavaSE 8?
 
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
OPP2010 (Brussels) - Programming with XML in PL/SQL - Part 1
 

Mehr von torp42

Mehr von torp42 (6)

The DE-9IM Matrix in Details using ST_Relate: In Picture and SQL
The DE-9IM Matrix in Details using ST_Relate: In Picture and SQLThe DE-9IM Matrix in Details using ST_Relate: In Picture and SQL
The DE-9IM Matrix in Details using ST_Relate: In Picture and SQL
 
Spatial Indexing
Spatial IndexingSpatial Indexing
Spatial Indexing
 
Entity-Relationship Diagrams ERD
Entity-Relationship Diagrams ERDEntity-Relationship Diagrams ERD
Entity-Relationship Diagrams ERD
 
Temporal Databases: Data Models
Temporal Databases: Data ModelsTemporal Databases: Data Models
Temporal Databases: Data Models
 
Temporal Databases: Modifications
Temporal Databases: ModificationsTemporal Databases: Modifications
Temporal Databases: Modifications
 
Temporal Databases: Queries
Temporal Databases: QueriesTemporal Databases: Queries
Temporal Databases: Queries
 

Kürzlich hochgeladen

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 

XML on SQL Server

  • 1. XML on SQL Server Kristian Torp Department of Computer Science Aalborg University people.cs.aau.dk/˜torp torp@cs.aau.dk November 5, 2015 daisy.aau.dk Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 1 / 69
  • 2. Outline 1 Create and Insert 2 XML Queries Saying Hello exist() query() value() nodes() 3 Generating XML from Select Statements Raw Auto Path 4 Summary Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 2 / 69
  • 3. Learning Goals Goals Overview of XML support on SQL Server Storing XML directly SQL Server Querying XML on SQL Server Standard Concepts are (fairly) general Code is DBMS specific DBMS Versions Tested SQL Server 2008 SQL Server 2012 Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 3 / 69
  • 4. Create the Relational Schema Example (Create the Table) create table course ( id i n t primary key , dsc xml not n u l l ) Note A create table statement as we know it The XML data type named xml A mixture of “old” data types int and new data types xml The not-null constraint, just because we love them! Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 5 / 69
  • 5. Insert Data Example (Insert a Row) i n s e r t i n t o course values (1 , ’<courses> <course id =”22” ects =”5” > <name>XML</name> <description >XPath and XQuery f o r querying XML documents </ description > </course> <course id =”11” ects =”5” > <name>DB</name> <description >Querying a r e l a t i o n a l database </ description > </course> </courses> ’ ) Note A plain insert statement The XML data type coersed from the text string Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 6 / 69
  • 6. Insert Additional Row Example (Insert another Row) i n s e r t i n t o course values (2 , ’<courses> <course id =”44” ects =”10”> <name>Imperative Programming </name> <description >C and Pointers </ description > </course> <course id =”55” ects =”5” > <name>OOP</name> <description >Working with classes and objects </ description > </course> </courses> ’ ) Note Nothing new conceptually, simply an insert statement We now have a small database! Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 7 / 69
  • 7. Database Content Example id dsc 1 <courses> <course id=” 22 ” ects=” 5 ”> <name>XML</name> <description>XPath . . . </ description> </ course> <course id=” 11 ” ects=” 5 ”> <name>DB</name> <description>Querying . . . </ description> </ course> </ courses> 2 <courses> <course id=” 44 ” ects=” 10 ”> <name>Imperative Programming</name> <description>C . . . </ description> </ course> <course id=” 55 ” ects=” 5 ”> <name>OOP</name> <description>Working . . . </ description> </ course> </ courses> Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 8 / 69
  • 8. Not Well-formed XML Example (Insert a Row) i n s e r t i n t o course values (3 , ’<course id =”77” ects =”10”> <name>Object−Oriented Analysis and Design </name> <description >Modeling with classes and objects </ description > </course> <course id =”88” ects =”5” > <name>Software Architecture </name> <description >The big picture </ description > </course> ’ ) Note Has no root element This works fine! Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 9 / 69
  • 9. Not Well-formed XML, cont. Example (End tag is missing) i n s e r t i n t o course values (4 , ’<course id =”99” ects =”10”> <name>Operating Systems </name> <description >Linux , Windows , and so on</ description > ’ ) Note XML parsing: line 3, character 61, unexpected end of input Example (Fix the missing end tag) i n s e r t i n t o course values (4 , ’<course id =”99” ects =”10”> <name>Operating Systems </name> <description >Linux , Windows , and so on</ description > </course> ’ ) Note Works! Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 10 / 69
  • 10. Not Well-formed XML, cont. Example (Nesting Wrong) i n s e r t i n t o course values (5 , ’<course id =”13” ects =”5” > <name><more>Compiler Construction </name></more> <description >Lex and Yacc</ description > </course> ’ ) Note XML parsing: line 2, character 41, end tag does not match start tag Example (“Weird” Cases) i n s e r t i n t o course values (6 , ’ ’ ) i n s e r t i n t o course values (7 , ’NULL ’ ) Note Works! Example (Does this work?) i n s e r t i n t o course values (8 ,NULL) Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 11 / 69
  • 11. Summary: Create and Insert Main Points Create table has new XML data type Very similar to the SQL/XML XML data type Insert statement works as expected XML documents, without root possible, tags must be matching Delete works as expected Not shown here There is special support for updating XML columns Updating only a part of the XML document in a cell Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 12 / 69
  • 12. Recall the Database Content Example id dsc 1 <courses> <course id=” 22 ” ects=” 5 ”> <name>XML</name> <description>XPath . . . </ description> </ course> <course id=” 11 ” ects=” 5 ”> <name>DB</name> <description>Querying . . . </ description> </ course> </ courses> 2 <courses> <course id=” 44 ” ects=” 10 ”> <name>Imperative Programming</name> <description>C . . . </ description> </ course> <course id=” 55 ” ects=” 5 ”> <name>OOP</name> <description>Working . . . </ description> </ course> </ courses> Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 15 / 69
  • 13. Say Hello Example (Use the Course Table) select dsc . query ( ” l e t $s := ’ Hello , World ! ’ return $s ” ) from course Note This does not work! And I used a long time to understand why not :-) Example (Try Again (almost the same)) select dsc . query ( ’ l e t $s := ” Hello , World ! ” return $s ’ ) from course Note Works! ’ and “ are different! dsc is a column on table course Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 16 / 69
  • 14. Say Hello, cont Example (Say Hello from XQuery) select dsc . query ( ’ l e t $s := ” Hello , World ! ” return $s ’ ) from course Example (Result) (No column name) Hello, World! Hello, World! Note A hello for each row in the table This is a simple XQuery SQL Server generally follows the XQuery standard well Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 17 / 69
  • 15. Say Hello, the T-SQL Way Example (Use a Variable) declare @x xml select @x. query ( ’ l e t $s := ” Hello , World ! ” return $s ’ ) Example (Result) (No column name) null Example (Initialize the Variable) declare @x xml = ’ ’ select @x. query ( ’ l e t $s := ” Hello , World ! ” return $s ’ ) Example (Result) (No column name) Hello, World! Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 18 / 69
  • 16. Document IDs with more than 5 ECTS Courses Example (Use exist() in where ) select id from course where dsc . e x i s t ( ’ / / course [ @ects > 5] ’ ) Note An expression of non-boolean type specified in a context where a condition is expected, near ’)’ Example (Use exist() in select ) select id , dsc . e x i s t ( ’ / / course [ @ects > 5] ’ ) from course Example (Result) id (No column name) 1 0 2 1 Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 20 / 69
  • 17. Retrying Document IDs with more than 5 ECTS Courses Example (Use exist() in where ) select id from course where dsc . e x i s t ( ’ / / course [ @ects > 5] ’ ) = 1 Example (Result) id 2 Note exist() returns 0, 1 or null This is a surprise! (when used to other DBMSs) Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 21 / 69
  • 18. Recall the Database Content Example id dsc 1 <courses> <course id=” 22 ” ects=” 5 ”> <name>XML</name> <description>XPath . . . </ description> </ course> <course id=” 11 ” ects=” 5 ”> <name>DB</name> <description>Querying . . . </ description> </ course> </ courses> 2 <courses> <course id=” 44 ” ects=” 10 ”> <name>Imperative Programming</name> <description>C . . . </ description> </ course> <course id=” 55 ” ects=” 5 ”> <name>OOP</name> <description>Working . . . </ description> </ course> </ courses> Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 23 / 69
  • 19. XPath for finding Course Names Example (Use XPath) select dsc . query ( ’ / courses / course /name ’ ) as res from course Example (Result) res <name>XML</name> <name>DB</name> <name>Imperative Programming</name> <name>OOP</name> null Note What was expected, it is really XPath! A null value for each row where the XPath has no match Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 24 / 69
  • 20. XPath for finding Course Names in Text Format Example (Use text()) select dsc . query ( ’ / courses / course /name/ t e x t ( ) ’ ) as res from course Example (Result) res XMLDB Imperative ProgrammingOOP Note What was expected! Not really useful in an SQL context Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 25 / 69
  • 21. Accessing Attributes Example (Look at Attributes) select dsc . query ( ’ / courses / course / @ects ’ ) as res from course Note XQuery [course.dsc.query()]: Attribute may not appear outside of an element Unexpected! Example (Look at Attributes) select dsc . query ( ’sum ( / courses / course / @ects ) ’ ) as res from course Example (Result) res 10 15 Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 26 / 69
  • 22. More XPath Example (Find 5 ECTS Courses) select dsc . query ( ’ / courses / course [ @ects =5]/name/ t e x t ( ) ’ ) as res from course Example (Result) res XMLDB OOP Example (Filtering using where ) select dsc . query ( ’ / / course /name/ t e x t ( ) ’ ) as res from course where id = 1 Example (Result) res XMLDB Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 27 / 69
  • 23. Multi-Column XPath Example (More than One Column) select dsc . query ( ’ / / course /name ’ ) as name, dsc . query ( ’ / / course / description ’ ) as descr from course Example (Result) name descr <name>XML</name> <name>DB</name> <description>XPath . . . </ description> <description>Querying . . . </ description> <name>Imperative . . . </name> <name>OOP</name> <description>C and Pointers</ description> <description>Working . . . </ description> Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 28 / 69
  • 24. Multi-Column XPath with text() Example (Multiple Columns and text()) select dsc . query ( ’ / / course /name/ t e x t ( ) ’ ) as name, dsc . query ( ’ / / course / description / t e x t ( ) ’ ) as descr from course Example (Result) name descr XMLDB XPath ... Querying ... Imperative Programming OOP C and Pointers Working ... Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 29 / 69
  • 25. Full FLWOR Example (FLWOR Query) select dsc . query ( ’ f o r $c in / / course where $c / @ects >= 5 return ( $c /name, $c / description ) ’ ) as res from course Example (Result) res <name>XML</name> <description>XPath and XQuery f o r querying XML documents</ description> <name>DB</name> <description>Querying a r e l a t i o n a l database</ description> <name>Imperative Programming</name> <description>C and Pointers</ description> <name>OOP</name> <description>Working with classes and objects</ description> Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 30 / 69
  • 26. Recall the Database Content Example id dsc 1 <courses> <course id=” 22 ” ects=” 5 ”> <name>XML</name> <description>XPath . . . </ description> </ course> <course id=” 11 ” ects=” 5 ”> <name>DB</name> <description>Querying . . . </ description> </ course> </ courses> 2 <courses> <course id=” 44 ” ects=” 10 ”> <name>Imperative Programming</name> <description>C . . . </ description> </ course> <course id=” 55 ” ects=” 5 ”> <name>OOP</name> <description>Working . . . </ description> </ course> </ courses> Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 32 / 69
  • 27. Get Course Names in a Relational Fashion Example (Using the value() Function) select dsc . value ( ’ / / course /name ’ , ’ nvarchar (30) ’ ) from course Note XQuery [course.dsc.value()]: ’value()’ requires a singleton (or empty sequence), found operand of type ’xdt:untypedAtomic *’ Example (Singleton’ing) select dsc . value ( ’ ( / / course /name ) [ 1 ] ’ , ’ nvarchar (30) ’ ) from course Example (Result) (No column name) XML Imperative Programming Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 33 / 69
  • 28. Retrying Get Course Names in a Relational Fashion Example (First) select dsc . value ( ’ ( / / course [ 1 ] / name ) [ 1 ] ’ , ’ nvarchar (30) ’ ) from course Example (Result) (No column name) XML Imperative Programming Example (Last) select dsc . value ( ’ ( / / course /name ) [ l a s t ( ) ] ’ , ’ nvarchar (30) ’ ) from course Example (Result) (No column name) DB OOP Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 34 / 69
  • 29. Value and Wrong SQL Data Type Example (First) select dsc . value ( ’ ( / / course /name ) [ 1 ] ’ , ’ nvarchar (30) ’ ) from course Example (Result) (No column name) XML Imperative Programming Example (Last) select dsc . value ( ’ ( / / course /name ) [ 1 ] ’ , ’ nvarchar (3) ’ ) from course Example (Result) (No column name) XML Imp Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 35 / 69
  • 30. value() and Wrong SQL Data Type, cont. Example (String as an Integer) select dsc . value ( ’ ( / / course /name ) [ 1 ] ’ , ’ i n t ’ ) from course Note Conversion failed when converting the nvarchar value ’XML’ to data type int. Example (Integer Attribute) select dsc . value ( ’ ( / / course / @ects ) [ 1 ] ’ , ’ i n t ’ ) from course Example (Result) (No column name) 5 10 Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 36 / 69
  • 31. Flattening using value() Example (String as an Integer) select id , dsc . value ( ’ ( / / course / @ects ) [ 1 ] ’ , ’ i n t ’ ) as ects from course where dsc . e x i s t ( ’ / / course [ @ects > 5] ’ ) = 1 Example (Result) id ects 2 10 Note Using value() and exist() functions in same query Still too few rows Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 37 / 69
  • 32. Flattening using value(), cont Example (String as an Integer) select dsc . value ( ’ ( / / course / @id ) [ l a s t ( ) ] ’ , ’ i n t ’ ) as cid , dsc . value ( ’ ( / / course /name ) [ l a s t ( ) ] ’ , ’ nvarchar (30) ’ ) as cname , dsc . value ( ’ ( / / course / description ) [ l a s t ( ) ] ’ , ’ nvarchar (50) ’ ) as dsc . value ( ’ ( / / course / @ects ) [ l a s t ( ) ] ’ , ’ i n t ’ ) as ects from course Example (Result) cid cname dsc ects 11 DB Querying... 5 55 OOP Working... 5 Note Pure relational world result Result only has two rows (should have four) value() cannot be used must use nodes() Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 38 / 69
  • 33. Flattening using nodes() Example (Flatten XML) select f l a t . course . value ( ’ ( . / @id ) [ 1 ] ’ , ’ i n t ’ ) as cid , f l a t . course . value ( ’ ( . / name ) [ 1 ] ’ , ’ varchar (50) ’ ) as cname , f l a t . course . value ( ’ ( . / description ) [ 1 ] ’ , ’ varchar (50) ’ ) as dsc , f l a t . course . value ( ’ ( . / @ects ) [ 1 ] ’ , ’ i n t ’ ) as ects from course cross apply dsc . nodes ( ’ / / course ’ ) f l a t ( course ) order by cid Example (Result) cid cname dsc ects 11 DB Querying... 5 22 XML XPath... 5 44 Imp. C and ... 10 55 OOP Working... 5 Note Relational view on XML data!!! Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 40 / 69
  • 34. Hide XML Example (Create a View on XML Data) create view coursexml flat as select f l a t . course . value ( ’ ( . / @id ) [ 1 ] ’ , ’ i n t ’ ) as cid , f l a t . course . value ( ’ ( . / name ) [ 1 ] ’ , ’ varchar (50) ’ ) as cname , f l a t . course . value ( ’ ( . / description ) [ 1 ] ’ , ’ varchar (50) ’ ) as dsc , f l a t . course . value ( ’ ( . / @ects ) [ 1 ] ’ , ’ i n t ’ ) as ects from course cross apply dsc . nodes ( ’ / / course ’ ) f l a t ( course ) Example (Query the View) select ∗ from coursexml flat where ects = 5 and cid > 20 order by cid Example (Result) cid cname dsc ects 22 XML XPath... 5 55 OOP Working... 5 Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 41 / 69
  • 35. Summary: Queries Functions Function Descrition exist() Check for existence query() XPath and XQuery value() Extract single value nodes() For flatting nodes modify() For (fine grained) updates of XML Note SQL Server only SQL/XML via programming languages Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 42 / 69
  • 36. Master-Detail Example I Order id customer name amount OrderLine lineno price per unit quan. desc. has Assumptions An order cannot exist without order lines Amount is the sum of the amount of order lines for a give order Must be larger than zero Derived from order lines but stored because often queried Order line is a weak entity Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 44 / 69
  • 37. Master-Detail Example II Example create table oorder ( oid i n t primary key , cname varchar (30) not n u l l ) Example create table oorder line ( oid i n t not null , line no i n t not n u l l check ( line no > 0) , dsc varchar (50) not null , quantity i n t not n u l l check ( quantity > 0) , price each numeric (6 ,2) not n u l l check ( price each > 0.0) , c o n s t r a i n t ol pk primary key ( oid , line no ) , c o n s t r a i n t o l o f k foreign key ( oid ) references oorder ( oid ) ) Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 45 / 69
  • 38. Get XML Out Example select ∗ from oorder f o r xml raw Example (Result) <row oid=” 11 ” cname=”Anna” /> <row oid=” 22 ” cname=” Benny ” /> <row oid=” 33 ” cname=” Curt ” /> Note A select statement The for xml raw is SQL Server specific Pretty ugly format ;-) Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 47 / 69
  • 39. Get Elements Out Example select ∗ from oorder f o r xml raw , elements Example (Result) <row> <oid>11</ oid> <cname>Anna</ cname> </ row> <row> <oid>22</ oid> <cname>Benny</ cname> </ row> <row> <oid>33</ oid> <cname>Curt</ cname> </ row> Note Much nicer Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 48 / 69
  • 40. Rename Element Names Example select oid as ” order −id ” , cname as ” customer−name” from oorder f o r xml raw , elements Example (Result) <row> <order −id>11</ order −id> <customer−name>Anna</ customer−name> </ row> <row> <order −id>22</ order −id> <customer−name>Benny</ customer−name> </ row> snip Note A select statement The for xml raw is SQL Server specific Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 49 / 69
  • 41. Rename Enclosed Element Example select oid as ” order −id ” , cname as ” customer−name” from oorder f o r xml raw ( ’ customer ’ ) , elements Example (Result) <customer> <order −id>11</ order −id> <customer−name>Anna</ customer−name> </ customer> <customer> <order −id>33</ order −id> <customer−name>Curt</ customer−name> </ customer> Note All columns are elements Missing a root element Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 50 / 69
  • 42. Adding a Root Element Example select oid as ” order −id ” , cname as ” customer−name” from oorder f o r xml raw ( ’ customer ’ ) , elements , root ( ’ customers ’ ) Example (Result) <customers> <customer> <order −id>11</ order −id> <customer−name>Anna</ customer−name> </ customer> <customer> <order −id>33</ order −id> <customer−name>Curt</ customer−name> </ customer> </ customers> Note Now nice XML document Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 51 / 69
  • 43. Joining Tables Example select ∗ from oorder , oorder line where oorder . oid = oorder line . oid f o r xml raw , elements Example (Result (snip)) <row> <oid>11</ oid> <cname>Anna</ cname> <oid>11</ oid> <line no>1</ line no> <dsc>Cola</ dsc> <quantity>1</ quantity> <price each>10.00</ price each> </ row> Note No nesting Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 52 / 69
  • 44. Summary: Raw Overview Fast to get XML fragments out Nice simple extension to select statement Default output in attributes Either all attributes or all elements Mixture of attributes and elements hard Is a bit “raw” Additional options, type, xmldata, xmlschema Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 53 / 69
  • 45. Get XML Out Example select ∗ from oorder f o r xml auto Example (Result) <row oid=” 11 ” cname=”Anna” /> <row oid=” 22 ” cname=” Benny ” /> <row oid=” 33 ” cname=” Curt ” /> Note At first glanse looks like for xml raw But wait, there is more ... Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 55 / 69
  • 46. The Power of Auto Example select ∗ from oorder , oorder line where oorder . oid = oorder line . oid and oid < 30 f o r xml auto Example (Result) <oorder oid=” 11 ” cname=”Anna”> <o order line oid=” 11 ” line no =” 1 ” dsc=” Cola ” quantity =” 1 ” price each=” 10.00 ” <o order line oid=” 11 ” line no =” 2 ” dsc=” Chips ” quantity =” 1 ” price each=” 15.00 </ oorder> <oorder oid=” 22 ” cname=” Benny ”> <o order line oid=” 22 ” line no =” 1 ” dsc=” Cola ” quantity =” 1 ” price each=” 10.00 ” <o order line oid=” 22 ” line no =” 2 ” dsc=” Burger ” quantity =” 2 ” price each=” 20.0 <o order line oid=” 22 ” line no =” 3 ” dsc=” Chips ” quantity =” 1 ” price each=” 15.00 </ oorder> Note Does automatic nesting at “natural” levels Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 56 / 69
  • 47. Rename Elements Example select oorder . oid as ” order ” , oorder . cname as ” customer ” , oorder line . dsc as ” description ” , oorder line . quantity , oorder line . price each as ” price ” from oorder , oorder line where oorder . oid = oorder line . oid f o r xml auto Example (Result (snip)) <oorder order=” 11 ” customer=”Anna”> <o order line description =” Cola ” quantity =” 1 ” price=” 10.00 ” /> <o order line description =” Chips ” quantity =” 1 ” price=” 15.00 ” /> </ oorder> <oorder order=” 22 ” customer=” Benny ”> <o order line description =” Cola ” quantity =” 1 ” price=” 10.00 ” /> <o order line description =” Burger ” quantity =” 2 ” price=” 20.00 ” /> <o order line description =” Chips ” quantity =” 1 ” price=” 15.00 ” /> </ oorder> Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 57 / 69
  • 48. Add Root Element + Rename Example select ” order ” . oid as ” id ” , ” order ” . cname as ” customer ” , ” l i n e ” . dsc as ” description ” , ” l i n e ” . quantity , ” l i n e ” . price each as ” price ” from oorder as ” order ” , oorder line as ” l i n e ” where ” order ” . oid = ” l i n e ” . oid f o r xml auto , root ( ’ orders ’ ) Example (Result (snip)) <orders> <order id=” 11 ” customer=”Anna”> <l i n e description =” Cola ” quantity =” 1 ” price=” 10.00 ” /> <l i n e description =” Chips ” quantity =” 1 ” price=” 15.00 ” /> </ order> <order id=” 22 ” customer=” Benny ”> <l i n e description =” Cola ” quantity =” 1 ” price=” 10.00 ” /> <l i n e description =” Burger ” quantity =” 2 ” price=” 20.00 ” /> <l i n e description =” Chips ” quantity =” 1 ” price=” 15.00 ” /> </ order> </ orders> Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 58 / 69
  • 49. Make it All Elements Example select ” order ” . oid as ” id ” , ” order ” . cname as ” customer ” , ” l i n e ” . dsc as ” description ” , ” l i n e ” . quantity , ” l i n e ” . price each as ” price ” from oorder as ” order ” , oorder line as ” l i n e ” where ” order ” . oid = ” l i n e ” . oid f o r xml auto , elements , root ( ’ orders ’ ) Example (Result (snip)) <orders> <order> <id>11</ id> <customer>Anna</ customer> <l i n e> <description>Cola</ description> <quantity>1</ quantity> <price>10.00</ price> </ l i n e> snip Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 59 / 69
  • 50. Summary: Auto Overview Fast to get XML out even for joins Row tag can only be renamed by table alias Okay at getting nesting correct Either attributes or elements Options similar as for the xml raw output Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 60 / 69
  • 51. Get XML Out Example select oorder . oid as ” id ” , oorder . cname as ” customer ” , oorder line . line no as ” lineno ” , oorder line . dsc as ” description ” , oorder line . quantity , oorder line . price each as ” price ” from oorder , oorder line where oorder . oid = oorder line . oid f o r xml path Example (Result (snip)) <row> <id>11</ id> <customer>Anna</ customer> <lineno>1</ lineno> <description>Cola</ description> <quantity>1</ quantity> <price>10.00</ price> </ row> Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 62 / 69
  • 52. Adding Structure Example select oorder . oid as ” id ” , oorder . cname as ” customer ” , oorder line . line no as ” lineno ” , oorder line . dsc as ” description ” , oorder line . quantity , oorder line . price each as ” price ” from oorder , oorder line where oorder . oid = oorder line . oid f o r xml path ( ’ order ’ ) , root ( ’ orders ’ ) Example (Result (snip)) <orders> <order> <id>11</ id> <customer>Anna</ customer> <lineno>1</ lineno> <description>Cola</ description> <quantity>1</ quantity> <price>10.00</ price> </ order> <order> Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 63 / 69
  • 53. Adding Attributes and Nesting Line Items Example select oorder . oid as ”@id” , oorder . cname as ” customer ” , oorder line . line no as ” l i n e / lineno ” , oorder line . dsc as ” l i n e / description ” , oorder line . quantity as ” l i n e / quantity ” , oorder line . price each as ” l i n e / price ” from oorder , oorder line where oorder . oid = oorder line . oid f o r xml path ( ’ order ’ ) , root ( ’ orders ’ ) Example (Result (snip)) <orders> <order id=” 11 ”> <customer>Anna</ customer> <l i n e> <lineno>1</ lineno> <description>Cola</ description> <quantity>1</ quantity> <price>10.00</ price> </ l i n e> </ order> Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 64 / 69
  • 54. Nesting Correctly Example select oorder . oid as ”@id” , oorder . cname as ” customer ” , ( select oorder line . line no as ” @lineno ” , oorder line . dsc as ” description ” , oorder line . quantity as ” quantity ” , oorder line . price each as ” price ” from oorder line where oorder . oid = oorder line . oid f o r xml path ( ’ l i n e ’ ) , type ) from oorder f o r xml path ( ’ order ’ ) , root ( ’ orders ’ ) Example (Result (snip)) <orders> <order id=” 11 ”> <customer>Anna</ customer> <l i n e lineno=” 1 ”> <description>Cola</ description> <quantity>1</ quantity> <price>10.00</ price> </ l i n e> Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 65 / 69
  • 55. Summary: Auto Overview More complicated than raw and auto mode Nesting “correctly” by subqueries Cast to XML type otherwise a string! Relies on naming conventions for attributes versus elements Options similar as for the raw and auto modes Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 66 / 69
  • 56. Summary Summary XPath follows the standard XQuery follows the standard XML support on SQL Server far from standard! Not Looked At (RTFM :-)) Indexing XML columns Updates Querying XML from C# SQLXML (Microsoft specific) Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 68 / 69
  • 57. Additional Information Web Sites Introduction to XQuery in SQL Server 2005 Good overview, cannot find a newer msdn.microsoft.com/en-us/library/ms345122(v=sql.90).aspx XML Options in Microsoft SQL Server 2005 More on the XML data type, cannot find a newer msdn.microsoft.com/en-us/library/ms345110%28v=sql.90%29. aspx SQLXML 4.0 Programming Concepts Good tutorial Must be installed separately on SQL Server 2008 and above msdn.microsoft.com/en-us/library/ms171779.aspx FOR XML (SQL Server) SQL Server 2012 http://msdn.microsoft.com/en-us/library/ms178107.aspx Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 69 / 69