Storing and Querying XML Data in SQL Server
Storing and Querying XML Data in SQL Server
• XML Document
• One top-level root element
• XML Fragment
• Multiple top-level elements
• Well-formed XML will include at least a prolog,
one top-level element, and correctly nested
element tags
XML Namespaces
• Use Cases
• Processing XML from external applications
• Structure of the data is undefined
• Improve the interoperability of the data
• An explicit sequence within the data needs to be
maintained
• Indexing and improved querying is required on the
XML
Demonstration: Introduction to XML and XML
Schemas
XML Data
XML Schema Collections
Untyped vs. Typed XML
CONTENT vs. DOCUMENT
• Demonstration: Working with Typed vs. Untyped
XML
XML Data
• Explicit mode
• Enables tabular representation of XML documents
• Enables complete control of the XML format
• Requires complex SQL statements that can be more
easily generated with other modes
Using Path Mode Queries
What is XQuery?
The query() Method
The value() Method
The exist() Method
The modify() Method
• Demonstration: XQuery Methods in a DDL Trigger
What is XQuery?
• Example:
SELECT BusinessEntitylD
FROM Sales.Store
WHERE Demographics.exist(
‘/StoreSurvey[NumberEmployees=14]’
The modify() Method
SELECT person.BusinessEntityID,
person.LastName,
contact.value('act:number','nvarchar(4000)') As
ChangeInContactNumber
FROM Person.Person AS person
CROSS APPLY person.AdditionalContactInfo.nodes(
'/AdditionalContactInfo/act:telephoneNumber‘
) AS helpdesk(contact)
WHERE person.AdditionalContactInfo IS NOT NULL;
Demonstration: Shredding XML
Review Question(s)
• Best Practice