Download presentation
Presentation is loading. Please wait.
1
Dirt, Spit, and Happy FLWOR
Hands on with XQuery Dirt, Spit, and Happy FLWOR
2
Presentation Rating: ADVANCED INTERMEDIATE BEGINNER
3
Presentation Rating:
4
Purpose Introduce the basic syntax of FLWOR, the XQuery language.
Interfaces with T-SQL Basic functions We won’t be covering conversion of SQL to XML (FOR XML…) No tricks or tips
5
Why Me? Data Architect working in the field of financial information security Prior experience as DBA and a reporting/database developer AtlantaMDF Chapter Leader Tutor, not a teacher
6
Assumptions About You Database professional (DBA, database developer, BI specialist) Some experience with XML
7
Let’s Review “Gentlemen, this is a football.” – Vince Lombardi
8
Basic XML Structure <root> <tag>This is an element</tag> <tag att=“attribute”> Another element</tag> </root> Elements and Attributes are both nodes.
9
Basic XML Structure XML can be Typed or Untyped
Untyped = xml fragments; WYSIWYG Typed = validation of datatypes, relationships Typed XML requires a Schema SQL Server supports Schema SQL Server supports inline DTD Namespaces Used for providing uniquely named elements and attributes in an XML document
10
XML in SQL Server 2000+ Generation: Translation: FOR XML OPENXML
RAW, AUTO, EXPLICIT Translation: OPENXML sp_xml_preparedocument sp_xml_removedocument
11
XML in SQL Server 2005+ Generation: Translation: FOR XML xml datatype
PATH TYPE Translation: xml datatype XQuery
12
XML Translation xml datatype Well-formed fragments (no root required)
2 GB maximum Cannot be compared or sorted Supports conversion to (n)varchar(max) Required for XQuery
13
XML Translation XQuery Complete query language outside of SQL Server
SQL Server implements limited subset xml methods exist() value() query() nodes() modify() (beyond scope of presentation)
14
XML Translation SQL engine interfaces with XML engine
15
XML Translation XML method Syntax Returns .exist() .exist(XQuery) bit: 1 if True, 0 if False, NULL if xml is NULL .value() .value(XQuery, SQLtype) SQL data type: specified as a parameter in method .query() .query(XQuery) XML: 1 fragment per row .nodes() .nodes(XQuery) as Table(column) XML: each node becomes a new row Primary purpose of XQuery in SQL Server is to convert XML data to a tuple in a dataset. For robust handling of XML, use another tool.
16
FLWOR “Any fool can write code that a computer can understand. Good programmers write code that humans can understand.” – Martin Fowler
17
FLWOR For Let Where Order By Return Context switches ({})
Primary Language Other Components For Let Where Order By Return Context switches ({}) Functions (data(), position()) Positional References ([ ]) SQL Server’s implementation of XQuery is a limited subset; it improved between 2005 and 2008, but several functions are still not available.
18
Positional References []
XML = '<Families> <Family name="Ainsworth"> <Parent name="Stuart"> <Child name="Isabel"/> <Child name="Grace"/> </Parent> </Family> <Family name="Smith"> <Parent name="Aaron"> <Child name="John"/> </Families>' SELECT 'varchar(100)'), 'varchar(100)')
19
Context switches {} XML = '<Families> <Family name="Ainsworth"> <Parent name="Stuart"> <Child name="Isabel"/> <Child name="Grace"/> </Parent> </Family> <Family name="Smith"> <Parent name="Aaron"> <Child name="John"/> </Families>'
20
Context switches {} <New> <Family name="Ainsworth"> <Parent name="Stuart"> <Child name="Isabel" /> <Child name="Grace" /> </Parent> </Family> </New>
21
(for)(let)… return FLWOR requires either for or let and a return statement return returns the actual dataset {for $f in /Families return "Hello World!"} </root> ') <root>Hello World!</root>
22
for for is an iterative operator
Used to assign elements into a variable Step through that variable {for $f in /Family return "Hello World!"} </root> ') <root>Hello World! Hello World!</root>
23
let let assigns a value to a variable
Introduced in SQL 2008 $f in //Family let return $n} </root> ') <root>Ainsworth Smith</root>
24
Pop Quiz! <Families> <Family name="Ainsworth"> <Parent name="Stuart" /> </Family> <Family name="Smith"> <Parent name="Aaron" /> </Families> $f in //Family let for $p in $f/Parent return " ", $n)}</Person>} </root>')
25
Pop Quiz! <root> <Person>Stuart Ainsworth</Person> <Person>Aaron Smith</Person> </root>
26
where Similar to WHERE clause in SQL
XML = '<Families> <Family name="Ainsworth"> <Parent name="Stuart"> <Child name="Isabel"/> <Child name="Grace"/> </Parent> </Family> <Family name="Smith"> <Parent name="Aaron"> <Child name="John"/> </Families>'
27
where <root> <Person>Isabel Ainsworth</Person>
$f in //Family let for $c in $f/Parent/Child where $n="Ainsworth" return " ", $n)}</Person>} </root>') <root> <Person>Isabel Ainsworth</Person> <Person>Grace Ainsworth</Person> </root>
28
order by SELECT @x.query('<root>{for $f in //Family
let for $c in $f/Parent/Child where $n="Ainsworth“ order by return " ", $n)}</Person>} </root>') <root> <Person>Grace Ainsworth</Person> <Person>Isabel Ainsworth</Person> </root>
29
More stuff to learn “A child of five would understand this. Send someone to fetch a child of five.” - Groucho Marx
30
Functions ceiling, floor, round concat, contains, substring
lower-case Function (XQuery) string-length upper-case Function (XQuery) not number local-name Function (XQuery) namespace-uri Function (XQuery) last position empty distinct-values id Function (XQuery) count avg, min, max, sum string, data true Function (XQuery) false Function (XQuery) expanded-QName (XQuery) local-name-from-QName (XQuery) namespace-uri-from-QName (XQuery) sql:column() function (XQuery) sql:variable() function (XQuery)
31
sql:variable() XML = '<Loc id="1">Atlanta</Loc> <Loc INTEGER = 1 $l in /Loc where return data($l)}</root>') $l in /Loc where <root>Washington</root> <root>Atlanta</root>
32
sql:column() XML = '<Loc id="1">Atlanta</Loc> <Loc id="2">Washington</Loc>' TABLE (ID INTEGER) INSERT (ID) VALUES(1), (2) $l in /Loc where return data($l)}</root>') T _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ <root>Atlanta</root> <root>Washington</root>
33
Pop Quiz! DECLARE @x XML = '<row>A</row>
<row>B</row> <row>C</row> <row>D</row> <row>E</row> ' TABLE (ID INT) INSERT (ID) SELECT ROW_NUMBER() OVER (ORDER BY c.column_id ) FROM sys.columns c [position()=sql:column("T.ID")]') t WHERE t.ID {count(/row)} </r>').value('.', 'int')
34
Pop Quiz! <row>A</row> <row>B</row>
<row>C</row> <row>D</row> <row>E</row>
35
Questions? “It’s a miracle that curiosity survives formal education.” – Albert Einstein
36
Contact Information Stuart R Ainsworth
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.