sql2005 xml
TRANSCRIPT
XML Features in SQL Server 2005
David Silverlight [MVP XML][email protected]
Overview
XML Enhancements in SQL Server 2005
The xml Data Type
Using XQuery
SQL Server 2005 XML Enhancements
Enhancements to the FOR XML Clause
Enhancements to the OPENXML Function
Enhancements to the FOR XML Clause
Enhancement Description
ELEMENTS directive in RAW mode
RAW mode queries can return element-centric XML.
Support for NULL values
Element-centric results can include empty elements for null values.
Inline XSD schemas You can generate inline XSD schemas.
TYPE directive returns results as xml data type
Results from FOR XML queries can be xml values, making nested queries possible.
PATH modeYou can use XPath-like expressions to define XML results.
ROOT directive You can specify a root element for your results.
Demos: Using the FOR XML Clause
Return elements in RAW mode
Return NULL values
Return an inline XSD schema
Use the TYPE directive
Use PATH mode
Enhancements to the OPENXML Function
Enhancement Description
Documents can be xml data type values
The sp_xml_preparedocument stored procedure accepts xml parameters.
Support for xml data type in the WITH clause
Columns of type xml can be returned in the WITH clause.
Batch-level scopingDocument handles are scoped at the batch level and are released when the batch is completed.
The xml Data Type
Storing XML in the Database
How to Use Untyped XML
How to Use Typed XML
How to Manage XML Schemas
Storing XML in the Database
Benefits:Benefits:
Single store for structured and semistructured dataDefine variable content in a relational modelChoose the most appropriate data model
Single store for structured and semistructured dataDefine variable content in a relational modelChoose the most appropriate data model
Functionality:Functionality:
XML IndexesXQuery-based data retrievalXQuery-based modifications
XML IndexesXQuery-based data retrievalXQuery-based modifications
XML schema support:XML schema support:Typed XML: Validated by a schemaUntyped XML: Nonvalidated XML
Typed XML: Validated by a schemaUntyped XML: Nonvalidated XML
How to Use Untyped XML
CREATE TABLE Invoices(InvoiceID int, SalesDate datetime, CustomerID int, ItemList xml)
DECLARE @itemDoc xml
CREATE TABLE Invoices(InvoiceID int, SalesDate datetime, CustomerID int, ItemList xml)
DECLARE @itemDoc xmlSET @itemDoc = '<Items>etc.</Items>'SET @itemDoc = '<Items>etc.</Items>'SET @itemDoc = CAST('<Items>etc.</Items>') AS xmlSET @itemDoc = CAST('<Items>etc.</Items>') AS xmlSET @itemDoc = CONVERT(xml,'<Items>etc.</Items>')SET @itemDoc = CONVERT(xml,'<Items>etc.</Items>')
SET @itemDoc = CONVERT(xml,'<Items>etc.')ERROR!
SET @itemDoc = CONVERT(xml,'<Items>etc.')ERROR!
Declare xml data typeDeclare xml data type
Implicitly cast string valuesImplicitly cast string values
Explicitly cast string valuesExplicitly cast string values
Use well-formed XMLUse well-formed XML
Explicitly convert string valuesExplicitly convert string values
How to Manage XML Schemas
CREATE XML SCHEMA COLLECTION cvSchemasASN'<?xml version="1.0" ?> <xsd:schema targetNamespace= ...'
CREATE XML SCHEMA COLLECTION cvSchemasASN'<?xml version="1.0" ?> <xsd:schema targetNamespace= ...'
SELECT * FROM sys.xml_schema_collections
SELECT * FROM sys.xml_namespaces
SELECT * FROM sys.xml_schema_collections
SELECT * FROM sys.xml_namespacesALTER XML SCHEMA COLLECTION cvSchemasALTER XML SCHEMA COLLECTION cvSchemasDROP XML SCHEMA COLLECTION cvSchemasDROP XML SCHEMA COLLECTION cvSchemas
Create an XML schema collectionCreate an XML schema collection
View schema informationView schema information
Modify a schema collectionModify a schema collection
Remove a schema collectionRemove a schema collection
How to Use Typed XML
Declare a typed column or variableDeclare a typed column or variable
CREATE TABLE HumanResources.EmployeeResume(EmployeeID int, Resume xml (cvSchemas))
CREATE TABLE HumanResources.EmployeeResume(EmployeeID int, Resume xml (cvSchemas))
INSERT INTO HumanResources.EmployeeResume VALUES (1, '<?xml version="1.0" ?> <resume xmlns="http://cvSchemas"> ...</resume>')
INSERT INTO HumanResources.EmployeeResume VALUES (1, '<?xml version="1.0" ?> <resume xmlns="http://cvSchemas"> ...</resume>')
Assign typed XML (must conform to schema)Assign typed XML (must conform to schema)
Use CONTENT or DOCUMENT to allow/disallow fragmentsUse CONTENT or DOCUMENT to allow/disallow fragments
CREATE TABLE HumanResources.EmployeeResume(EmployeeID int, Resume xml (DOCUMENT cvSchemas))
CREATE TABLE HumanResources.EmployeeResume(EmployeeID int, Resume xml (DOCUMENT cvSchemas))
Demos: Using Typed XML
Create an XML schema collection
View XML schema collections
View XML namespaces
Create a table with a typed xml column
Insert valid XML
Attempt to insert invalid XML
Use the CONTENT and DOCUMENT keywords
Using XQuery
What Is XQuery?
How to Query XML with xml Data Type Methods
How to Modify XML with the modify Method
How to Shred XML to Relational Format with the Nodes Method
What Is XQuery?
Query language to identify nodes in XMLQuery language to identify nodes in XML
/InvoiceList/Invoice[@InvoiceNo=1000]/InvoiceList/Invoice[@InvoiceNo=1000]
FLOWR statements (For, Let, Order by, Where and Return )FLOWR statements (For, Let, Order by, Where and Return )
Statement Description
for Iterate through sibling nodes.
where Apply filtering criteria to the iteration.
return Specify the XML to be returned.
Demos: Using XQuery Expressions
Examine an XML document
Use simple XQuery path expressions
Use an XQuery condition
Use the for and return statements
Generate literal XML
Return attributes and values
Use for, where, and return statements
How to Query XML with xml Data Type Methods
SELECT xmlCol.query( '<InvoiceNumbers> { for $i in /InvoiceList/Invoice return <InvoiceNo> {number($i/@InvoiceNo)} </InvoiceNo> } </InvoiceNumbers>')
SELECT xmlCol.query( '<InvoiceNumbers> { for $i in /InvoiceList/Invoice return <InvoiceNo> {number($i/@InvoiceNo)} </InvoiceNo> } </InvoiceNumbers>')
SELECT xmlCol.value('(/InvoiceList/Invoice/@InvoiceNo)[1]', 'int')
SELECT xmlCol.value('(/InvoiceList/Invoice/@InvoiceNo)[1]', 'int')
SELECT xmlCol.exist('/InvoiceList/Invoice[@InvoiceNo=1000]')
SELECT xmlCol.exist('/InvoiceList/Invoice[@InvoiceNo=1000]')
SELECT Invoices.query( '<Store> {sql:column("StoreName")} </Store>')
SELECT Invoices.query( '<Store> {sql:column("StoreName")} </Store>')
Use the query methodUse the query method
Use the value methodUse the value method
Bind relational columns and variablesBind relational columns and variables
Use the exist methodUse the exist method
How to Modify XML with the modify Method
SET @xmlDoc.modify( 'insert element salesperson {"Bill"} as first into (/InvoiceList/Invoice)[1]')
SET @xmlDoc.modify( 'insert element salesperson {"Bill"} as first into (/InvoiceList/Invoice)[1]')
SET xmlCol.modify( replace value of(/InvoiceList/Invoice/SalesPerson/text())[1] with "Ted"')
SET xmlCol.modify( replace value of(/InvoiceList/Invoice/SalesPerson/text())[1] with "Ted"')
SET @xmlDoc.modify( 'delete (/InvoiceList/Invoice/SalesPerson)[1]')
SET @xmlDoc.modify( 'delete (/InvoiceList/Invoice/SalesPerson)[1]')
Use the insert statementUse the insert statement
Use the replace statementUse the replace statement
Use the delete statementUse the delete statement
What we have learned
XML is thoroughly integrated into SQL Server 2005.
For XML and OpenXML have seen become much more powerful.
Schemas are respected as part of table updates and definitions
XQuery is a very powerful query language that allows you to Query your existing data types.
Thank You!!!
David Silverlight
http://www.xmlpitstop.com
http://www.nonprofitways.com