Download presentation
Presentation is loading. Please wait.
1
Using XML in SQL Server and Azure SQL Database
Pete Harris | Senior Content Developer, Microsoft Graeme Malcolm | Senior Content Developer, Microsoft
2
Meet Your Instructors Pete Harris | @peteatmsft
Senior Content Devloper Various roles at Microsoft since 1995 Graeme Malcolm | Senior Content Developer at Microsoft Consultant, trainer, and author since SQL Server 4.2
3
Course Topics Using XML in SQL Server and Azure SQL Database
01 | Introduction to XML Data 02 | Storing and Querying XML 03 | Implementing XML Indexes 04 | Working with Typed XML 05 | Creating Relational Data from XML 06 | Creating XML from Relational Data
4
Setting Expectations Target Audience
SQL Server database developers Anyone pursuing Microsoft certification in SQL Server Suggested Prerequisites/Supporting Material Querying with Transact-SQL MVA course
5
Introduction to XML Data
Pete Harris | Senior Content Developer, Microsoft Graeme Malcolm | Senior Content Developer, Microsoft
6
Module Overview What is XML? Representing Data with XML XML Namespaces
XML Support in SQL Server and Azure SQL Database
7
What is XML? Based on SGML Describes data structures and values
Commonly used by applications for: Data interchange Configuration Data storage Case-sensitive <?xml version="1.0" encoding="ISO "?> <order id="123456" date=" "> <salesperson id="123"> <name>Naomi Sharp</name> </salesperson> <customer id="921"> <name>Dan Drayton</name> </customer> <items> <item id="561" quantity="1"/> <item id="127" quantity="2"/> </items> </order>
8
Representing Data with XML Basic XML Structure
<?xml version="1.0" encoding="ISO "?> <order id="123456" date=" "> <salesperson id="123"> <name>Naomi Sharp</name> </salesperson> <customer id="921"> <name>Dan Drayton</name> </customer> <!-- an order may contain multiple items --> <items> <item id="561" quantity="1"/> <item id="127" quantity="2"/> </items> </order> element processing instruction text comment shorthand syntax for closing empty elements attribute – order is not enforced
9
An XML Document
10
Representing Data with XML Node Trees
/ order id date customer name items item quantity salesperson <?xml version="1.0" encoding="ISO "?> <order id="123456" date=" "> <salesperson id="123"> <name>Naomi Sharp</name> </salesperson> <customer id="921"> <name>Dan Drayton</name> </customer> <!-- an order may contain multiple items --> <items> <item id="561" quantity="1"/> <item id="127" quantity="2"/> </items> </order>
11
Representing Data with XML Documents vs Fragments
Documents are well-formed They have a single root element Fragments have no root element But all elements in the fragment are well-formed <employee id="123"> <first-name>Naomi</first-name> <last-name>Sharp</last-name> </employee> <product id="561" price =" ">Mountain Bike</product> <product id="127" price ="9.59">Water Bottle</product>
12
XML Namespaces <items>
<?xml version="1.0" encoding="ISO "?> <o:order xmlns:o=" xmlns:s=" xmlns:c=" xmlns:p=" o:id="123456" o:date=" "> <s:salesperson s:id="123"> <s:name>Naomi Sharp</s:name> </s:salesperson> <c:customer c:id="921"> <c:name>Dan Drayton</c:name> </c:customer> <!-- an order may contain multiple items --> <o:items> <o:item p:id="561" o:quantity="1"/> <o:item p:id="127" o:quantity="2"/> </o:items> </o:order> <?xml version="1.0" encoding="ISO "?> <order id="123456" date=" "> <salesperson id="123"> <name>Naomi Sharp</name> </salesperson> <customer id="921"> <name>Dan Drayton</name> </customer> <!-- an order may contain multiple items --> <items> <item id="561" quantity="1"/> <item id="127" quantity="2"/> </items> </order> <?xml version="1.0" encoding="ISO "?> <order xmlns=" id="123456" date=" "> <salesperson xmlns=" id="123"> <name>Naomi Sharp</name> </salesperson> <customer xmlns=" id="921"> <name>Dan Drayton</name> </customer> <!-- an order may contain multiple items --> <items> <item id="561" quantity="1"/> <item id="127" quantity="2"/> </items> </order>
13
XML Support In SQL Server and Azure SQL Database
Feature Description xml data type Store XML in variables and columns XQuery Node-tree query language for XML XML Indexes Optimize XQuery performance XSD Schemas Enforce type validation for XML data FOR XML clause Generate XML from relational data OPENXML function Generate relational data from XML
14
XML Support Scenarios for XML in a Database
Why work with XML data in SQL Server and Azure SQL Database? You need to share, query, and modify XML in an efficient and transacted way You have both relational and XML data and need to have interoperability You need to build cross-domain applications and need portability of data Your data is sparse or you do not know the structure of your data You want the server to guarantee that the XML is well-formed and optionally validate your data against a schema You want to index your XML data for high query performance You want to store and query relational data, but use XML as an interchange format
15
Introduction to XML Data
What is XML? Representing Data with XML XML Namespaces XML Support in SQL Server and Azure SQL Database
16
Storing and Querying XML
Pete Harris | Senior Content Developer, Microsoft Graeme Malcolm | Senior Content Developer, Microsoft
17
Module Overview The xml Data Type XQuery Querying the xml Data Type
Modifying the xml Data Type
18
The xml Data Type Native data type for XML
Enables you to store XML documents and fragments Used for columns, variables, or parameters Stores XML in an internal form When queried, semantically equivalent XML is returned
19
The xml Data Type
20
XQuery Basic Syntax Query syntax for XML Based on XPath expressions
/ order id date customer name items item quantity salesperson <?xml version="1.0" encoding="ISO "?> <order id="123456" date=" "> <salesperson id="123"> <name>Naomi Sharp</name> </salesperson> <customer id="921"> <name>Dan Drayton</name> </customer> <!-- an order may contain multiple items --> <items> <item id="268" quantity="2"/> <item id="561" quantity="1"/> <item id="127" quantity="2"/> </items> </order> /order/salesperson/name /order//name /order/items/item[1] > 1]
21
XQuery FLWOR Queries For, (Let), Where, Order By, Return
<?xml version="1.0" encoding="ISO "?> <order id="123456" date=" "> <salesperson id="123"> <name>Naomi Sharp</name> </salesperson> <customer id="921"> <name>Dan Drayton</name> </customer> <!-- an order may contain multiple items --> <items> <item id="268" quantity="2"/> <item id="561" quantity="1"/> <item id="127" quantity="2"/> </items> </order> <item id="127" quantity="2"/> <item id="268" quantity="2"/> for $i in /order/items/item where > 1 order by return $i
22
Querying the xml Data Type
The xml data type exposes methods support XQuery expressions query (xquery): Returns XML node(s) value (xquery, datatype): Returns a scalar value exist (xquery): Returns 1 (True) or 0 (False) to indicate existence of specified node nodes (xquery) AS table (column): Returns a rowset of XML nodes Often used with CROSS APPLY
23
Querying the xml Data Type
24
Modifying the xml Data Type
Use the modify method: modify (insert … into xquery) modify (replace xquery with …) modify (delete xquery) Note: When modifying XML in a table, the modify method is called in the SET clause of an UPDATE statement – regardless of the type of operation!
25
Modifying the xml Data Type
26
Storing and Querying XML
The xml Data Type XQuery Querying the xml Data Type Modifying the xml Data Type
27
Implementing XML Indexes
Pete Harris | Senior Content Developer, Microsoft Graeme Malcolm | Senior Content Developer, Microsoft
28
Module Overview Introduction to XML Indexes Primary XML Index
Secondary XML Indexes
29
Introduction to XML Indexes
XML data can be slow to access Node tree is created for each query XML indexes can help with query performance Pre-defined node tree Indexes contain details of: Nodes Values Paths / order id date customer name items item quantity salesperson Indexes are used in SQL Server to improve the performance of queries. XML indexes are used to improve the performance of XQuery-based queries. Many systems query XML data directly as text. This can be very slow, particularly if the XML data is large. You saw earlier how XML data is not directly stored in a text format in SQL Server. For ease of querying, it is broken into a form of object tree that makes it easier to navigate in memory. Rather than having to create these object trees as required for queries, which is also a relatively slow process, it is possible to define XML indexes. An XML index is rather like a copy of an XML object tree that is saved into the database for rapid reuse. It is important to note that XML indexes can be quite large compared to the underlying XML data. Relational indexes are often much smaller than the tables on which they are built, but it is not uncommon to see XML indexes that are larger than the underlying data. You should also consider alternatives to XML indexes. Promoting a value that is stored within the XML to a persisted calculated column would make it possible to use a standard relational index to quickly locate the value.
30
Primary XML Index Provides a persisted node tree in an internal format that is used to speed access to elements and attributes within the XML Requires a clustered primary key on the table CREATE PRIMARY XML INDEX XML_Order_Items ON Sales.Order (Items); The primary XML index basically provides a persisted object tree in an internal format. The tree has been formed from the structure of the XML, is used to speed up access to elements and attributes within the XML, and avoids the need to read the entire XML document for every query. Before you can create a primary XML index on a table, the table must have a clustered primary key.
31
Secondary XML Indexes Can only be created after a primary XML index has been created You can create three types of secondary indexes to help resolve specific XQuery expressions rapidly: PROPERTY – Optimized for retrieving multiple values in query method calls VALUE – Optimized for retrieving single values in value method calls PATH – Typically used by the exist method Most of the querying benefit comes from primary XML indexes, but SQL Server also enables the creation of three types of secondary XML index. These secondary indexes are each designed to speed up a particular type of query. There are three forms of query that they help with: PATH, VALUE, and PROPERTY: A PATH index helps to decide whether a particular path to an element or attribute is valid. It is typically used with the exist() XQuery method. A VALUE index helps to obtain the value of an element or attribute. A PROPERTY index is used when retrieving multiple values through PATH expressions. You can only create a secondary XML index after a primary XML index has been established. When you are creating the secondary XML index, you need to reference the primary XML index. CREATE XML INDEX XML_Order_Items_Property ON Sales.Order (Items) USING XML INDEX XML_Order_Items FOR PROPERTY;
32
Using XML Indexes
33
Implementing XML Indexes
Introduction to XML Indexes Primary XML Index Secondary XML Indexes
34
Working with Typed XML Pete Harris | Senior Content Developer, Microsoft Graeme Malcolm | Senior Content Developer, Microsoft
35
Module Overview XML Schemas XML Schema Collections
Untyped XML vs Typed XML Storing Fragments or Documents
36
XML Schema XML schema describes the structure of an XML document
XML schema language is also called XML Schema Definition (XSD) An XML schema provides the following: Validation constraints Data type information Information about types of attributes and elements
37
Validating XML with an XML Schema
38
XML Schema Collections
You can optionally associate XSD schemas with an xml data type through an XML schema collection The XML schema collection: Stores the imported XML schemas Validates xml data type instances Types the XML data as it is stored in the database
39
Untyped XML vs. Typed XML
Use the untyped xml data type when: You do not have a schema for your XML data You have schemas, but don’t want the server to validate data (there is significant impact on a server performing validation) Use the typed xml data type when: You have schemas and want the server to validate XML data You want to take advantage of storage and query optimizations based on type information You want to take advantage of type information during compilation of your queries
40
Storing Fragments or Documents
The xml data type stores content by default (including fragments) You can specify the DOCUMENT keyword to prevent storage of fragments
41
Using XML Schema Collections
42
Working with Typed XML XML Schemas XML Schema Collections
Untyped XML vs Typed XML Storing Fragments or Documents
43
05 | Creating Relational Data from XML
Pete Harris | Senior Content Developer, Microsoft Graeme Malcolm | Senior Content Developer, Microsoft
44
Module Overview Options for Shredding XML The nodes Method
sp_xml_preparedocument and sp_xml_removedocument OPENXML OPENXML with Namespaces XML Metadata Properties
45
Options for Shredding XML
The nodes method of the xml data type The OPENXML statement <?xml version="1.0“ ?> <order id="123456"> … </order> SELECT t.n.value(…) AS t(n) <?xml version="1.0“ ?> <order id="123456"> … </order> sp_xml_preparedocument SELECT * FROM OPENXML sp_xml_removedocument
46
Relative path to value node
The nodes Method Relative path to value node SELECT 'int') AS OrderID, 'date') AS OrderDate, OrderTable.OrderXml.value('./customer[1]/name[1]', 'nvarchar(25)') AS Customer AS OrderTable(OrderXml); Context node Use nodes in the FROM clause to specify a context node Creates a row for each instance of that node Use value in the SELECT clause to extract values from relative nodes (using XPath expressions)
47
Shredding XML with the nodes Method
48
sp_xml_preparedocument and sp_xml_removedocument
Use sp_xml_preparedocument to create a node tree Use sp_xml_removedocument to release memory int; EXEC EXEC
49
OPENXML Handle returned by sp_xml_preparedocument Context node SELECT * FROM 'order', 1) WITH (id int, date date, customerid varchar(25) 'customer/name'); Flags: 0: attribute-centric 1: attribute-centric 2: element-centric 3 (1+2): attributes and elements Explicit path to non-default node Note that the rowset schema in the WITH clause can be a table name if the flags enables all nodes to be located without an explicit column pattern. Specify document handle, row pattern to context node, and flags to indicate default centricity Specify rowset schema and explicit column patterns in the WITH clause
50
Using OPENXML
51
OPENXML with Namespaces
Specify namespace and prefix in sp_xml_preparedocument Additional parameter with <root> tag Prefix namespace in row and column patterns EXEC '<root xmlns:awo=" SELECT * FROM 'awo:order', 1) WITH (id int, date date, customer varchar(25) 'awo:customer/awo:name');
52
Using OPEN XML with a Namespace
53
XML Metadata Properties
Retrieving XML Metadata properties as a column pattern Creating overflow columns Use Flag + 8, retrieve non-mapped XML Creating an edge table Omit flags and WITH clause
54
Using Metadata Columns
55
Creating Relational Data from XML
Options for Shredding XML The nodes Method sp_xml_preparedocument and sp_xml_removedocument OPENXML OPENXML with Namespaces XML Metadata Properties
56
06 | Creating XML from Relational Data
Pete Harris | Senior Content Developer, Microsoft Graeme Malcolm | Senior Content Developer, Microsoft
57
Module Overview The FOR XML Clause RAW Mode AUTO Mode EXPLICIT Mode
PATH Mode Using Namespaces with FOR XML
58
The FOR XML Clause Extends SELECT syntax
Returns XML instead of rows and columns Is configurable to return attributes, elements, and the schema
59
RAW Mode Returns an XML representation of a rowset
Results can be element- or attribute-centric Specify an optional root element and row element name SELECT c.CustomerID AS CustID, c.CompanyName, soh.SalesOrderID, soh.OrderDate, soh.TotalDue FROM SalesLT.Customer AS c INNER JOIN SalesLT.SalesOrderHeader AS soh ON c.CustomerID = soh.CustomerID ORDER BY c.CustomerID FOR XML RAW('Order'), ROOT('Orders'), ELEMENTS;
60
Using RAW Mode
61
AUTO Mode Creates nested child-elements for joined tables
Elements are named to match table (or alias) Results can be element- or attribute-centric Specify an optional root element SELECT [Order].SalesOrderID, [Order].OrderDate, [LineItem].ProductID, [LineItem].OrderQty, FROM Sales.SalesOrderHeader AS [Order] INNER JOIN Sales.SalesOrderDetail AS LineItem ON LineItem.SalesOrderID = [Order].SalesOrderID ORDER BY [Order].SalesOrderID FOR XML AUTO, ROOT('Orders'), ELEMENTS;
62
Using AUTO Mode
63
EXPLICIT Mode Enables tabular representation of XML documents
Enables complete control of the XML structure SELECT 1 AS Tag, NULL AS Parent, SalesOrderID AS [Invoice!1!InvoiceNo], OrderDate AS [Invoice!1!Date], CustomerID AS [Invoice!1!CustomerID!Element], TotalDue AS [Invoice!1!TotalDue!Element] FROM SalesLT.SalesOrderHeader WHERE SalesOrderID = 71774 FOR XML EXPLICIT;
64
Using EXPLICIT Mode
65
PATH Mode Uses XML Path Language (X Path) to specify XML format
Enables the creation of nested data and specifies what should be exposed as an element or an attribute Easier to use than EXPLICIT mode SELECT o.SalesOrderID AS o.OrderDate AS o.CustomerID AS c.CompanyName AS 'customer', o.TotalDue AS 'totaldue' FROM SalesLT.SalesOrderHeader AS o JOIN SalesLT.Customer AS c ON o.CustomerID = c.CustomerID WHERE SalesOrderID = 71774 FOR XML PATH('invoice');
66
Using PATH Mode
67
Using Namespaces with FOR XML
Supported only for RAW, AUTO, and PATH modes Specify a namespace and prefix Explicitly name elements and attributes with prefix WITH XMLNAMESPACES (' AS ord) WITH XMLNAMESPACES (' AS ord) SELECT SalesOrderID AS 'ord:SalesOrderID', OrderDate AS 'ord:OrderDate', CustomerID AS 'ord:CustomerID', TotalDue AS 'ord:TotalDue' FROM SalesLT.SalesOrderHeader WHERE SalesOrderID = 71774 FOR XML RAW('ord:Order'), ELEMENTS;
68
Using Namespaces with FOR XML
69
Creating XML from Relational Data
The FOR XML Clause RAW Mode AUTO Mode EXPLICIT Mode PATH Mode Using Namespaces with FOR XML
70
Using XML in SQL Server and Azure SQL Database
01 | Introduction to XML Data 02 | Storing and Querying XML 03 | Implementing XML Indexes 04 | Working with Typed XML 05 | Creating Relational Data from XML 06 | Creating XML from Relational Data
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.