Sql2005 Xml

1,736 views
1,663 views

Published on

Published in: Business, Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,736
On SlideShare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
Downloads
18
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Sql2005 Xml

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

×