xml on sql server

57
XML on SQL Server Kristian Torp Department of Computer Science Aalborg University people.cs.aau.dk/˜torp [email protected] November 5, 2015 daisy.aau.dk Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 1 / 69

Upload: torp42

Post on 14-Apr-2017

941 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: XML on SQL Server

XML on SQL Server

Kristian Torp

Department of Computer ScienceAalborg University

people.cs.aau.dk/˜[email protected]

November 5, 2015

daisy.aau.dk

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 1 / 69

Page 2: XML on SQL Server

Outline

1 Create and Insert

2 XML QueriesSaying Helloexist()query()value()nodes()

3 Generating XML from Select StatementsRawAutoPath

4 Summary

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 2 / 69

Page 3: XML on SQL Server

Learning Goals

GoalsOverview of XML support on SQL Server

Storing XML directly SQL Server

Querying XML on SQL Server

StandardConcepts are (fairly) general

Code is DBMS specific

DBMS Versions TestedSQL Server 2008

SQL Server 2012

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 3 / 69

Page 4: XML on SQL Server

Create the Relational Schema

Example (Create the Table)create tab l e course (

i d i n t pr imary key ,dsc xml not n u l l )

NoteA 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

Page 5: XML on SQL Server

Insert Data

Example (Insert a Row)i n s e r t i n t o course values (1 ,’<courses><course i d =”22” ects =”5”>

<name>XML< /name><desc r i p t i on >XPath and XQuery f o r query ing XML documents</ desc r i p t i on >

</ course><course i d =”11” ects =”5”>

<name>DB< /name><desc r i p t i on >Querying a r e l a t i o n a l database < / desc r i p t i on >

</ course></ courses> ’ )

NoteA 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

Page 6: XML on SQL Server

Insert Additional Row

Example (Insert another Row)i n s e r t i n t o course values (2 ,’<courses><course i d =”44” ects =”10”>

<name> Impera t i ve Programming< /name><desc r i p t i on >C and Pointers < / desc r i p t i on >

</ course><course i d =”55” ects =”5”>

<name>OOP< /name><desc r i p t i on >Working wi th c lasses and objec ts < / desc r i p t i on >

</ course></ courses> ’ )

NoteNothing 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

Page 7: XML on SQL Server

Database ContentExample

id dsc

1

<courses><course i d = ” 22 ” ects= ” 5 ”>

<name>XML< / name><d e s c r i p t i o n>XPath . . .< / d e s c r i p t i o n>

< / course><course i d = ” 11 ” ects= ” 5 ”>

<name>DB< / name><d e s c r i p t i o n>Querying . . .< / d e s c r i p t i o n>

< / course>< / courses>

2

<courses><course i d = ” 44 ” ects= ” 10 ”>

<name> Impera t i ve Programming< / name><d e s c r i p t i o n>C . . .< / d e s c r i p t i o n>

< / course><course i d = ” 55 ” ects= ” 5 ”>

<name>OOP< / name><d e s c r i p t i o n>Working . . .< / d e s c r i p t i o n>

< / course>< / courses>

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 8 / 69

Page 8: XML on SQL Server

Not Well-formed XML

Example (Insert a Row)i n s e r t i n t o course values (3 ,’<course i d =”77” ects =”10”><name>Object−Oriented Ana lys is and Design < /name><desc r i p t i on >Modeling wi th c lasses and objec ts < / desc r i p t i on >

</ course><course i d =”88” ects =”5”>

<name>Software Arch i t ec tu re < /name><desc r i p t i on >The big p i c tu re < / desc r i p t i on >

</ course> ’ )

NoteHas no root element

This works fine!

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 9 / 69

Page 9: XML on SQL Server

Not Well-formed XML, cont.

Example (End tag is missing)i n s e r t i n t o course values (4 ,’<course i d =”99” ects =”10”><name>Operat ing Systems< /name><desc r i p t i on >Linux , Windows , and so on< / desc r i p t i on > ’ )

NoteXML 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 i d =”99” ects =”10”><name>Operat ing Systems< /name><desc r i p t i on >Linux , Windows , and so on< / desc r i p t i on >

</ course> ’ )

NoteWorks!

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 10 / 69

Page 10: XML on SQL Server

Not Well-formed XML, cont.

Example (Nesting Wrong)i n s e r t i n t o course values (5 ,’<course i d =”13” ects =”5”><name><more>Compiler Const ruct ion < /name></more><desc r i p t i on >Lex and Yacc< / desc r i p t i on >

</ course> ’ )

NoteXML 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 ’ )

NoteWorks!

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

Page 11: XML on SQL Server

Summary: Create and Insert

Main PointsCreate 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 matchingDelete works as expected

Not shown here

There is special support for updating XML columnsUpdating only a part of the XML document in a cell

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 12 / 69

Page 12: XML on SQL Server

Recall the Database ContentExample

id dsc

1

<courses><course i d = ” 22 ” ects= ” 5 ”>

<name>XML< / name><d e s c r i p t i o n>XPath . . .< / d e s c r i p t i o n>

< / course><course i d = ” 11 ” ects= ” 5 ”>

<name>DB< / name><d e s c r i p t i o n>Querying . . .< / d e s c r i p t i o n>

< / course>< / courses>

2

<courses><course i d = ” 44 ” ects= ” 10 ”>

<name> Impera t i ve Programming< / name><d e s c r i p t i o n>C . . .< / d e s c r i p t i o n>

< / course><course i d = ” 55 ” ects= ” 5 ”>

<name>OOP< / name><d e s c r i p t i o n>Working . . .< / d e s c r i p t i o n>

< / course>< / courses>

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 15 / 69

Page 13: XML on SQL Server

Say Hello

Example (Use the Course Table)s e l e c t dsc . query ( ” l e t $s := ’ Hel lo , World ! ’ r e t u r n $s ” )from course

NoteThis does not work!

And I used a long time to understand why not :-)

Example (Try Again (almost the same))s e l e c t dsc . query ( ’ l e t $s := ” Hel lo , World ! ” r e t u r n $s ’ )from course

NoteWorks!

’ and “ are different!

dsc is a column on table course

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 16 / 69

Page 14: XML on SQL Server

Say Hello, cont

Example (Say Hello from XQuery)s e l e c t dsc . query ( ’ l e t $s := ” Hel lo , World ! ” r e t u r n $s ’ )from course

Example (Result)

(No column name)

Hello, World!Hello, World!

NoteA 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

Page 15: XML on SQL Server

Say Hello, the T-SQL Way

Example (Use a Variable)dec lare @x xmls e l e c t @x. query ( ’ l e t $s := ” Hel lo , World ! ” r e t u r n $s ’ )

Example (Result)

(No column name)

null

Example (Initialize the Variable)dec lare @x xml = ’ ’s e l e c t @x. query ( ’ l e t $s := ” Hel lo , World ! ” r e t u r n $s ’ )

Example (Result)

(No column name)

Hello, World!

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 18 / 69

Page 16: XML on SQL Server

Document IDs with more than 5 ECTS Courses

Example (Use exist() in where )s e l e c t i dfrom coursewhere dsc . e x i s t ( ’ / / course [ @ects > 5] ’ )

NoteAn expression of non-boolean type specified in a context where acondition is expected, near ’)’

Example (Use exist() in select )s e l e c t id , dsc . e x i s t ( ’ / / course [ @ects > 5] ’ )from course

Example (Result)

id (No column name)

1 02 1

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 20 / 69

Page 17: XML on SQL Server

Retrying Document IDs with more than 5 ECTS Courses

Example (Use exist() in where )s e l e c t i dfrom coursewhere dsc . e x i s t ( ’ / / course [ @ects > 5] ’ ) = 1

Example (Result)

id

2

Noteexist() 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

Page 18: XML on SQL Server

Recall the Database ContentExample

id dsc

1

<courses><course i d = ” 22 ” ects= ” 5 ”>

<name>XML< / name><d e s c r i p t i o n>XPath . . .< / d e s c r i p t i o n>

< / course><course i d = ” 11 ” ects= ” 5 ”>

<name>DB< / name><d e s c r i p t i o n>Querying . . .< / d e s c r i p t i o n>

< / course>< / courses>

2

<courses><course i d = ” 44 ” ects= ” 10 ”>

<name> Impera t i ve Programming< / name><d e s c r i p t i o n>C . . .< / d e s c r i p t i o n>

< / course><course i d = ” 55 ” ects= ” 5 ”>

<name>OOP< / name><d e s c r i p t i o n>Working . . .< / d e s c r i p t i o n>

< / course>< / courses>

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 23 / 69

Page 19: XML on SQL Server

XPath for finding Course Names

Example (Use XPath)s e l e c t dsc . query ( ’ / courses / course / name ’ ) as resfrom course

Example (Result)

res<name>XML< / name><name>DB< / name><name> Impera t i ve Programming< / name><name>OOP< / name>

null

NoteWhat 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

Page 20: XML on SQL Server

XPath for finding Course Names in Text Format

Example (Use text())s e l e c t dsc . query ( ’ / courses / course / name / t e x t ( ) ’ ) as resfrom course

Example (Result)

res

XMLDBImperative ProgrammingOOP

NoteWhat was expected!

Not really useful in an SQL context

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 25 / 69

Page 21: XML on SQL Server

Accessing Attributes

Example (Look at Attributes)s e l e c t dsc . query ( ’ / courses / course / @ects ’ ) as resfrom course

NoteXQuery [course.dsc.query()]: Attribute may not appear outside of anelement

Unexpected!

Example (Look at Attributes)s e l e c t dsc . query ( ’sum ( / courses / course / @ects ) ’ ) as resfrom course

Example (Result)

res

1015

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 26 / 69

Page 22: XML on SQL Server

More XPath

Example (Find 5 ECTS Courses)s e l e c t dsc . query ( ’ / courses / course [ @ects = 5 ] /name / t e x t ( ) ’ ) as resfrom course

Example (Result)

res

XMLDBOOP

Example (Filtering using where )s e l e c t dsc . query ( ’ / / course / name / t e x t ( ) ’ ) as resfrom coursewhere i d = 1

Example (Result)

res

XMLDB

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 27 / 69

Page 23: XML on SQL Server

Multi-Column XPath

Example (More than One Column)s e l e c t dsc . query ( ’ / / course / name ’ ) as name,

dsc . query ( ’ / / course / d e s c r i p t i o n ’ ) as descrfrom course

Example (Result)

name descr<name>XML< / name><name>DB< / name>

<d e s c r i p t i o n>XPath . . .< / d e s c r i p t i o n><d e s c r i p t i o n>Querying . . .< / d e s c r i p t i o n>

<name> Impera t i ve . . .< / name><name>OOP< / name>

<d e s c r i p t i o n>C and Po in te rs< / d e s c r i p t i o n><d e s c r i p t i o n>Working . . .< / d e s c r i p t i o n>

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 28 / 69

Page 24: XML on SQL Server

Multi-Column XPath with text()

Example (Multiple Columns and text())s e l e c t dsc . query ( ’ / / course / name / t e x t ( ) ’ ) as name,

dsc . query ( ’ / / course / d e s c r i p t i o n / t e x t ( ) ’ ) as descrfrom 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

Page 25: XML on SQL Server

Full FLWOR

Example (FLWOR Query)s e l e c t dsc . query ( ’ f o r $c i n / / course

where $c / @ects >= 5r e t u r n ( $c / name, $c / d e s c r i p t i o n ) ’ ) as res

from course

Example (Result)

res<name>XML< / name><d e s c r i p t i o n>XPath and XQuery f o r query ing XML documents< / d e s c r i p t i o n><name>DB< / name><d e s c r i p t i o n>Querying a r e l a t i o n a l database< / d e s c r i p t i o n><name> Impera t i ve Programming< / name><d e s c r i p t i o n>C and Po in te rs< / d e s c r i p t i o n><name>OOP< / name><d e s c r i p t i o n>Working wi th c lasses and ob jec ts< / d e s c r i p t i o n>

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 30 / 69

Page 26: XML on SQL Server

Recall the Database ContentExample

id dsc

1

<courses><course i d = ” 22 ” ects= ” 5 ”>

<name>XML< / name><d e s c r i p t i o n>XPath . . .< / d e s c r i p t i o n>

< / course><course i d = ” 11 ” ects= ” 5 ”>

<name>DB< / name><d e s c r i p t i o n>Querying . . .< / d e s c r i p t i o n>

< / course>< / courses>

2

<courses><course i d = ” 44 ” ects= ” 10 ”>

<name> Impera t i ve Programming< / name><d e s c r i p t i o n>C . . .< / d e s c r i p t i o n>

< / course><course i d = ” 55 ” ects= ” 5 ”>

<name>OOP< / name><d e s c r i p t i o n>Working . . .< / d e s c r i p t i o n>

< / course>< / courses>

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 32 / 69

Page 27: XML on SQL Server

Get Course Names in a Relational Fashion

Example (Using the value() Function)s e l e c t dsc . value ( ’ / / course / name ’ , ’ nvarchar (30) ’ )from course

NoteXQuery [course.dsc.value()]: ’value()’ requires a singleton (or emptysequence), found operand of type ’xdt:untypedAtomic *’

Example (Singleton’ing)s e l e c t dsc . value ( ’ ( / / course / name ) [ 1 ] ’ , ’ nvarchar (30) ’ )from course

Example (Result)

(No column name)

XMLImperative Programming

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 33 / 69

Page 28: XML on SQL Server

Retrying Get Course Names in a Relational FashionExample (First)s e l e c t dsc . value ( ’ ( / / course [ 1 ] / name ) [ 1 ] ’ , ’ nvarchar (30) ’ )from course

Example (Result)

(No column name)

XMLImperative Programming

Example (Last)s e l e c t dsc . value ( ’ ( / / course / name ) [ l a s t ( ) ] ’ , ’ nvarchar (30) ’ )from course

Example (Result)

(No column name)

DBOOP

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 34 / 69

Page 29: XML on SQL Server

Value and Wrong SQL Data TypeExample (First)s e l e c t dsc . value ( ’ ( / / course / name ) [ 1 ] ’ , ’ nvarchar (30) ’ )from course

Example (Result)

(No column name)

XMLImperative Programming

Example (Last)s e l e c t dsc . value ( ’ ( / / course / name ) [ 1 ] ’ , ’ nvarchar ( 3 ) ’ )from course

Example (Result)

(No column name)

XMLImp

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 35 / 69

Page 30: XML on SQL Server

value() and Wrong SQL Data Type, cont.

Example (String as an Integer)s e l e c t dsc . value ( ’ ( / / course / name ) [ 1 ] ’ , ’ i n t ’ )from course

NoteConversion failed when converting the nvarchar value ’XML’ to datatype int.

Example (Integer Attribute)s e l e c t dsc . value ( ’ ( / / course / @ects ) [ 1 ] ’ , ’ i n t ’ )from course

Example (Result)

(No column name)

510

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 36 / 69

Page 31: XML on SQL Server

Flattening using value()

Example (String as an Integer)s e l e c t id ,

dsc . value ( ’ ( / / course / @ects ) [ 1 ] ’ , ’ i n t ’ ) as ectsfrom coursewhere dsc . e x i s t ( ’ / / course [ @ects > 5] ’ ) = 1

Example (Result)

id ects

2 10

NoteUsing value() and exist() functions in same query

Still too few rows

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 37 / 69

Page 32: XML on SQL Server

Flattening using value(), cont

Example (String as an Integer)s e l e c t 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 / d e s c r i p t i o n ) [ l a s t ( ) ] ’ , ’ nvarchar (50) ’ ) as dsc ,dsc . value ( ’ ( / / course / @ects ) [ l a s t ( ) ] ’ , ’ i n t ’ ) as ects

from course

Example (Result)

cid cname dsc ects

11 DB Querying... 555 OOP Working... 5

NotePure 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

Page 33: XML on SQL Server

Flattening using nodes()Example (Flatten XML)s e l e c t 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 ( ’ ( . / d e s c r i p t i o n ) [ 1 ] ’ , ’ varchar (50) ’ ) as dsc ,f l a t . course . value ( ’ ( . / @ects ) [ 1 ] ’ , ’ i n t ’ ) as ects

from coursecross apply dsc . nodes ( ’ / / course ’ ) f l a t ( course )

order by c id

Example (Result)

cid cname dsc ects

11 DB Querying... 522 XML XPath... 544 Imp. C and ... 1055 OOP Working... 5

NoteRelational view on XML data!!!

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 40 / 69

Page 34: XML on SQL Server

Hide XMLExample (Create a View on XML Data)create view c o u r s e x m l f l a t ass e l e c t 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 ( ’ ( . / d e s c r i p t i o n ) [ 1 ] ’ , ’ varchar (50) ’ ) as dsc ,f l a t . course . value ( ’ ( . / @ects ) [ 1 ] ’ , ’ i n t ’ ) as ects

from coursecross apply dsc . nodes ( ’ / / course ’ ) f l a t ( course )

Example (Query the View)s e l e c t ∗from c o u r s e x m l f l a twhere ects = 5 and c id > 20order by c id

Example (Result)

cid cname dsc ects

22 XML XPath... 555 OOP Working... 5

NoteRelational view on XML data!!!

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 41 / 69

Page 35: XML on SQL Server

Summary: Queries

Functions

Function Descrition

exist() Check for existencequery() XPath and XQueryvalue() Extract single valuenodes() For flatting nodesmodify() For (fine grained) updates of XML

NoteSQL Server only

SQL/XML via programming languages

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 42 / 69

Page 36: XML on SQL Server

Master-Detail Example I

Order

id

customer name

amount

OrderLine

lineno

price per unit

quan. desc.

has

AssumptionsAn order cannot exist without order linesAmount 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

Page 37: XML on SQL Server

Master-Detail Example II

Examplecreate tab l e oorder (

o id i n t pr imary key ,cname varchar (30) not n u l l )

Examplecreate tab l e o o r d e r l i n e (

o id i n t not n u l l ,l i n e n o i n t not n u l l check ( l i n e n o > 0) ,dsc varchar (50) not n u l l ,q u a n t i t y i n t not n u l l check ( q u a n t i t y > 0) ,p r ice each numeric (6 ,2 ) not n u l l check ( pr ice each > 0 . 0 ) ,c o n s t r a i n t o l pk pr imary key ( oid , l i n e n o ) ,c o n s t r a i n t o l o f k f o r e i g n key ( o id )

re ferences oorder ( o id ) )

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 45 / 69

Page 38: XML on SQL Server

Get XML Out

Examples e l e c t ∗from oorderf o r xml raw

Example (Result)<row oid= ” 11 ” cname= ” Anna ” /><row oid= ” 22 ” cname= ” Benny ” /><row oid= ” 33 ” cname= ” Curt ” />

NoteA 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

Page 39: XML on SQL Server

Get Elements Out

Examples e l e c t ∗from oorderf o r xml raw , elements

Example (Result)<row><o id>11< / o id><cname>Anna< / cname>

< / row><row><o id>22< / o id><cname>Benny< / cname>

< / row><row><o id>33< / o id><cname>Curt< / cname>

< / row>

NoteMuch nicer

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 48 / 69

Page 40: XML on SQL Server

Rename Element Names

Examples e l e c t o id as ” order− i d ” ,

cname as ” customer−name”from oorderf o r xml raw , elements

Example (Result)<row><order− i d>11< / order− i d><customer−name>Anna< / customer−name>

< / row><row><order− i d>22< / order− i d><customer−name>Benny< / customer−name>

< / row>sn ip

NoteA select statement

The for xml raw is SQL Server specific

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 49 / 69

Page 41: XML on SQL Server

Rename Enclosed Element

Examples e l e c t o id as ” order− i d ” ,

cname as ” customer−name”from oorderf o r xml raw ( ’ customer ’ ) , elements

Example (Result)<customer><order− i d>11< / order− i d><customer−name>Anna< / customer−name>

< / customer><customer><order− i d>33< / order− i d><customer−name>Curt< / customer−name>

< / customer>

NoteAll columns are elements

Missing a root element

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 50 / 69

Page 42: XML on SQL Server

Adding a Root Element

Examples e l e c t o id as ” order− i d ” ,

cname as ” customer−name”from oorderf o r xml raw ( ’ customer ’ ) , elements , roo t ( ’ customers ’ )

Example (Result)<customers><customer><order− i d>11< / order− i d><customer−name>Anna< / customer−name>

< / customer><customer><order− i d>33< / order− i d><customer−name>Curt< / customer−name>

< / customer>< / customers>

NoteNow nice XML document

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 51 / 69

Page 43: XML on SQL Server

Joining Tables

Examples e l e c t ∗from oorder , o o r d e r l i n ewhere oorder . o id = o o r d e r l i n e . o idf o r xml raw , elements

Example (Result (snip))<row><o id>11< / o id><cname>Anna< / cname><o id>11< / o id>< l i n e n o>1< / l i n e n o><dsc>Cola< / dsc><q u a n t i t y>1< / q u a n t i t y><pr ice each>10.00< / p r ice each>

< / row>

NoteNo nesting

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 52 / 69

Page 44: XML on SQL Server

Summary: Raw

OverviewFast to get XML fragments out

Nice simple extension to select statement

Default output in attributesEither 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

Page 45: XML on SQL Server

Get XML Out

Examples e l e c t ∗from oorderf o r xml auto

Example (Result)<row oid= ” 11 ” cname= ” Anna ” /><row oid= ” 22 ” cname= ” Benny ” /><row oid= ” 33 ” cname= ” Curt ” />

NoteAt 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

Page 46: XML on SQL Server

The Power of Auto

Examples e l e c t ∗from oorder , o o r d e r l i n ewhere oorder . o id = o o r d e r l i n e . o idand o id < 30f o r xml auto

Example (Result)<oorder o id= ” 11 ” cname= ” Anna ”><o o r d e r l i n e o id= ” 11 ” l i n e n o = ” 1 ” dsc= ” Cola ” q u a n t i t y = ” 1 ” pr ice each= ” 10.00 ” /><o o r d e r l i n e o id= ” 11 ” l i n e n o = ” 2 ” dsc= ” Chips ” q u a n t i t y = ” 1 ” pr ice each= ” 15.00 ” />

< / oorder><oorder o id= ” 22 ” cname= ” Benny ”><o o r d e r l i n e o id= ” 22 ” l i n e n o = ” 1 ” dsc= ” Cola ” q u a n t i t y = ” 1 ” pr ice each= ” 10.00 ” /><o o r d e r l i n e o id= ” 22 ” l i n e n o = ” 2 ” dsc= ” Burger ” q u a n t i t y = ” 2 ” pr ice each= ” 20.00 ” /><o o r d e r l i n e o id= ” 22 ” l i n e n o = ” 3 ” dsc= ” Chips ” q u a n t i t y = ” 1 ” pr ice each= ” 15.00 ” />

< / oorder>

NoteDoes automatic nesting at “natural” levels

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 56 / 69

Page 47: XML on SQL Server

Rename Elements

Examples e l e c t oorder . o id as ” order ” ,

oorder . cname as ” customer ” ,o o r d e r l i n e . dsc as ” d e s c r i p t i o n ” ,o o r d e r l i n e . quan t i t y ,o o r d e r l i n e . pr ice each as ” p r i ce ”

from oorder , o o r d e r l i n ewhere oorder . o id = o o r d e r l i n e . o idf o r xml auto

Example (Result (snip))<oorder order= ” 11 ” customer= ” Anna ”><o o r d e r l i n e d e s c r i p t i o n = ” Cola ” q u a n t i t y = ” 1 ” p r i ce = ” 10.00 ” /><o o r d e r l i n e d e s c r i p t i o n = ” Chips ” q u a n t i t y = ” 1 ” p r i ce = ” 15.00 ” />

< / oorder><oorder order= ” 22 ” customer= ” Benny ”><o o r d e r l i n e d e s c r i p t i o n = ” Cola ” q u a n t i t y = ” 1 ” p r i ce = ” 10.00 ” /><o o r d e r l i n e d e s c r i p t i o n = ” Burger ” q u a n t i t y = ” 2 ” p r i ce = ” 20.00 ” /><o o r d e r l i n e d e s c r i p t i o n = ” Chips ” q u a n t i t y = ” 1 ” p r i ce = ” 15.00 ” />

< / oorder>

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 57 / 69

Page 48: XML on SQL Server

Add Root Element + RenameExamples e l e c t ” order ” . o id as ” i d ” ,

” order ” . cname as ” customer ” ,” l i n e ” . dsc as ” d e s c r i p t i o n ” ,” l i n e ” . quan t i t y ,” l i n e ” . p r ice each as ” p r i ce ”

from oorder as ” order ” , o o r d e r l i n e as ” l i n e ”where ” order ” . o id = ” l i n e ” . o idf o r xml auto , roo t ( ’ orders ’ )

Example (Result (snip))<orders><order i d = ” 11 ” customer= ” Anna ”>< l i n e d e s c r i p t i o n = ” Cola ” q u a n t i t y = ” 1 ” p r i ce = ” 10.00 ” />< l i n e d e s c r i p t i o n = ” Chips ” q u a n t i t y = ” 1 ” p r i ce = ” 15.00 ” />

< / order><order i d = ” 22 ” customer= ” Benny ”>< l i n e d e s c r i p t i o n = ” Cola ” q u a n t i t y = ” 1 ” p r i ce = ” 10.00 ” />< l i n e d e s c r i p t i o n = ” Burger ” q u a n t i t y = ” 2 ” p r i ce = ” 20.00 ” />< l i n e d e s c r i p t i o n = ” Chips ” q u a n t i t y = ” 1 ” p r i ce = ” 15.00 ” />

< / order>< / orders>

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 58 / 69

Page 49: XML on SQL Server

Make it All Elements

Examples e l e c t ” order ” . o id as ” i d ” ,

” order ” . cname as ” customer ” ,” l i n e ” . dsc as ” d e s c r i p t i o n ” ,” l i n e ” . quan t i t y ,” l i n e ” . p r ice each as ” p r i ce ”

from oorder as ” order ” , o o r d e r l i n e as ” l i n e ”where ” order ” . o id = ” l i n e ” . o idf o r xml auto , elements , roo t ( ’ orders ’ )

Example (Result (snip))<orders><order>< i d>11< / i d><customer>Anna< / customer>< l i n e><d e s c r i p t i o n>Cola< / d e s c r i p t i o n><q u a n t i t y>1< / q u a n t i t y><p r i ce>10.00< / p r i c e>

< / l i n e>sn ip

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 59 / 69

Page 50: XML on SQL Server

Summary: Auto

OverviewFast 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

Page 51: XML on SQL Server

Get XML Out

Examples e l e c t oorder . o id as ” i d ” ,

oorder . cname as ” customer ” ,o o r d e r l i n e . l i n e n o as ” l i neno ” ,o o r d e r l i n e . dsc as ” d e s c r i p t i o n ” ,o o r d e r l i n e . quan t i t y ,o o r d e r l i n e . pr ice each as ” p r i ce ”

from oorder , o o r d e r l i n ewhere oorder . o id = o o r d e r l i n e . o idf o r xml path

Example (Result (snip))<row>< i d>11< / i d><customer>Anna< / customer>< l i neno>1< / l i neno><d e s c r i p t i o n>Cola< / d e s c r i p t i o n><q u a n t i t y>1< / q u a n t i t y><p r i ce>10.00< / p r i c e>

< / row>

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 62 / 69

Page 52: XML on SQL Server

Adding StructureExamples e l e c t oorder . o id as ” i d ” ,

oorder . cname as ” customer ” ,o o r d e r l i n e . l i n e n o as ” l i neno ” ,o o r d e r l i n e . dsc as ” d e s c r i p t i o n ” ,o o r d e r l i n e . quan t i t y ,o o r d e r l i n e . pr ice each as ” p r i ce ”

from oorder , o o r d e r l i n ewhere oorder . o id = o o r d e r l i n e . o idf o r xml path ( ’ order ’ ) , r oo t ( ’ orders ’ )

Example (Result (snip))<orders><order>< i d>11< / i d><customer>Anna< / customer>< l i neno>1< / l i neno><d e s c r i p t i o n>Cola< / d e s c r i p t i o n><q u a n t i t y>1< / q u a n t i t y><p r i ce>10.00< / p r i c e>

< / order><order>

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 63 / 69

Page 53: XML on SQL Server

Adding Attributes and Nesting Line ItemsExamples e l e c t oorder . o id as ”@id ” ,

oorder . cname as ” customer ” ,o o r d e r l i n e . l i n e n o as ” l i n e / l i neno ” ,o o r d e r l i n e . dsc as ” l i n e / d e s c r i p t i o n ” ,o o r d e r l i n e . q u a n t i t y as ” l i n e / q u a n t i t y ” ,o o r d e r l i n e . pr ice each as ” l i n e / p r i ce ”

from oorder , o o r d e r l i n ewhere oorder . o id = o o r d e r l i n e . o idf o r xml path ( ’ order ’ ) , r oo t ( ’ orders ’ )

Example (Result (snip))<orders><order i d = ” 11 ”><customer>Anna< / customer>< l i n e>< l i neno>1< / l i neno><d e s c r i p t i o n>Cola< / d e s c r i p t i o n><q u a n t i t y>1< / q u a n t i t y><p r i ce>10.00< / p r i c e>

< / l i n e>< / order>

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 64 / 69

Page 54: XML on SQL Server

Nesting CorrectlyExamples e l e c t oorder . o id as ”@id ” ,

oorder . cname as ” customer ” ,( s e l e c t o o r d e r l i n e . l i n e n o as ” @lineno ” ,

o o r d e r l i n e . dsc as ” d e s c r i p t i o n ” ,o o r d e r l i n e . q u a n t i t y as ” q u a n t i t y ” ,o o r d e r l i n e . pr ice each as ” p r i ce ”

from o o r d e r l i n ewhere oorder . o id = o o r d e r l i n e . o idf o r xml path ( ’ l i n e ’ ) , type )

from oorderf o r xml path ( ’ order ’ ) , r oo t ( ’ orders ’ )

Example (Result (snip))<orders><order i d = ” 11 ”><customer>Anna< / customer>< l i n e l i neno = ” 1 ”><d e s c r i p t i o n>Cola< / d e s c r i p t i o n><q u a n t i t y>1< / q u a n t i t y><p r i ce>10.00< / p r i c e>

< / l i n e>Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 65 / 69

Page 55: XML on SQL Server

Summary: Auto

OverviewMore complicated than raw and auto modeNesting “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

Page 56: XML on SQL Server

Summary

SummaryXPath 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

Page 57: XML on SQL Server

Additional Information

Web SitesIntroduction to XQuery in SQL Server 2005

Good overview, cannot find a newermsdn.microsoft.com/en-us/library/ms345122(v=sql.90).aspx

XML Options in Microsoft SQL Server 2005More on the XML data type, cannot find a newermsdn.microsoft.com/en-us/library/ms345110%28v=sql.90%29.

aspx

SQLXML 4.0 Programming ConceptsGood tutorialMust be installed separately on SQL Server 2008 and abovemsdn.microsoft.com/en-us/library/ms171779.aspx

FOR XML (SQL Server)SQL Server 2012http://msdn.microsoft.com/en-us/library/ms178107.aspx

Kristian Torp (Aalborg University) XML on SQL Server November 5, 2015 69 / 69