2.2 SQL Server 2005 的 XML 支援功能. Overview XML Enhancements in SQL Server 2005 The xml Data Type Using XQuery.

  • Published on
    24-Dec-2015

  • View
    223

  • Download
    0

Transcript

  • Slide 1
  • 2.2 SQL Server 2005 XML
  • Slide 2
  • Overview XML Enhancements in SQL Server 2005 The xml Data Type Using XQuery
  • Slide 3
  • Lesson: XML Enhancements in SQL Server 2005 Enhancements to the FOR XML Clause Enhancements to the OPENXML Function
  • Slide 4
  • Enhancements to the FOR XML Clause EnhancementDescription 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 mode You can use XPath-like expressions to define XML results. ROOT directive You can specify a root element for your results. Named Element You can specify a named element for RAW and PATH mode queries.
  • Slide 5
  • Practice: Using the FOR XML Clause In this practice, you will: Return elements in RAW mode Return NULL values Return an inline XSD schema Use the TYPE directive Use PATH mode Use the ROOT directive Return named elements in a RAW mode query
  • Slide 6
  • Enhancements to the OPENXML Function EnhancementDescription 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 scoping Document handles are scoped at the batch level and are released when the batch is completed.
  • Slide 7
  • Practice: Using the OPENXML Function In this practice, you will execute the OPENXML function
  • Slide 8
  • Lesson: The xml Data Type Storing XML in the Database How to Use Untyped XML How to Manage XML Schemas How to Use Typed XML How to Manage XML Indexes
  • Slide 9
  • Storing XML in the Database Benefits: Single store for structured and semistructured data Define variable content in a relational model Choose the most appropriate data model Single store for structured and semistructured data Define variable content in a relational model Choose the most appropriate data model Functionality: XML Indexes XQuery-based data retrieval XQuery-based modifications XML Indexes XQuery-based data retrieval XQuery-based modifications XML schema support: Typed XML: Validated by a schema Untyped XML: Nonvalidated XML Typed XML: Validated by a schema Untyped XML: Nonvalidated XML
  • Slide 10
  • How to Use Typed XML Declare 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, '... ') INSERT INTO HumanResources.EmployeeResume VALUES (1, '... ') Assign typed XML (must conform to schema) Use 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))
  • Slide 11
  • Lesson: 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
  • Slide 12
  • What Is XQuery? Describe the XML nodes to be retrieved Syntax includes and extends XPath 2.0 Based on working draft of W3C XQuery 1.0 language Specification (http://www.w3.org/XML/Qeury) Consist of two main sections: Prolog Namespace declare and schema imported Body XQuery expression specify the data to be retrieved
  • Slide 13
  • Martin Gudgin 33 short Simon Horrell 40 short Mark Szolkowski 30 medium Sample input XML document; people.xml
  • Slide 14
  • XQuery FLWOR Expressions FOR LET (not supported by SQL Server 2005) WHERE ORDER BY RETURN Using XQuery with the XML data type Methods xml.exist xml.value xml.query xml.nodes xml.modify Updating the XML data type with XQuery DML
  • Slide 15
  • XQuery Expressions XQuery is a superset of XPath (: this is a valid XQuery :) /people/person[age > 30] (: so is this FLWOR expression :) for $p in /people/person where $p/age > 30 order by $p/age[1] return $p/name
  • Slide 16
  • Order by clause XQuery can easily sort by using order by similar to SQL ORDER BY clause must resolve to a scalar value nodesets not comparable
  • Slide 17
  • XQuery FLWOR Expressions FOR LET (not supported by SQL Server 2005) WHERE ORDER BY RETURN Using XQuery with the XML data type Methods xml.exist xml.value xml.query xml.nodes xml.modify Updating the XML data type with XQuery DML
  • Slide 18
  • SQL Server 2005 Support Method of XQuery XQuery is supported through methods on the XML type xml.exist - returns bool xml.value - returns scalar xml.query - returns XML data type instance xml.nodes - returns one column rowset w/XML column xml.modify - modifies an instance These methods can return columns in rowsets - when used with SQL SELECT variables
  • Slide 19
  • How to Query XML with xml Data Type Methods SELECT xmlCol.value( '(/InvoiceList/Invoice/@InvoiceNo)[1]', 'int') SELECT xmlCol.value( '(/InvoiceList/Invoice/@InvoiceNo)[1]', 'int') Use the value method
  • Slide 20
  • Using xml.exist -- pdoc must have a person element -- as a child of the people root CREATE TABLE xmltab( id INTEGER PRIMARY KEY, pdoc XML CHECK (pdoc.exist('/people/person')=1) ) -- ok insert xmltab values( 1, ' ') -- fails, no persons insert xmltab values( 2, ' ')
  • Slide 21
  • 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 statement Use the replace statement Use the delete statement

Recommended

View more >