2.2 sql server 2005 的 xml 支援功能. overview xml enhancements in sql server 2005 the xml data...
TRANSCRIPT
2.2 SQL Server 2005 的 XML 支援功能
Overview
XML Enhancements in SQL Server 2005
The xml Data Type
Using XQuery
Lesson: XML Enhancements in SQL Server 2005
Enhancements to the FOR XML Clause
Enhancements to the OPENXML Function
Enhancements to the FOR XML Clause
Enhancement DescriptionELEMENTS directive in RAW mode RAW mode queries can return element-centric XML.
Support for NULL valuesElement-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.
Named ElementYou can specify a named element for RAW and PATH mode queries.
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
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.
Practice: Using the OPENXML Function
In this practice, you will execute the OPENXML function
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
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 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))
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
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
<people> <person> <name> <givenName>Martin</givenName> <familyName>Gudgin</familyName> </name> <age>33</age> <height>short</height> </person> <person> <name> <givenName>Simon</givenName> <familyName>Horrell</familyName> </name> <age>40</age> <height>short</height> </person> <person> <name> <givenName>Mark</givenName> <familyName>Szolkowski</familyName> </name> <age>30</age> <height>medium</height> </person></people>
Sample input XML document; people.xml
XQuery
FLWOR ExpressionsFORLET (not supported by SQL Server 2005)WHEREORDER BYRETURN
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
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/personwhere $p/age > 30order by $p/age[1]return $p/name
Order by clause
XQuery can easily sort by using order by
similar to SQL ORDER BY clause
must resolve to a scalar valuenodesets not comparable
XQuery
FLWOR ExpressionsFORLET (not supported by SQL Server 2005)WHEREORDER BYRETURN
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
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
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 methodUse the value method
Using xml.exist
-- pdoc must have a person element-- as a child of the people rootCREATE TABLE xmltab( id INTEGER PRIMARY KEY, pdoc XML CHECK (pdoc.exist('/people/person')=1))-- okinsert xmltab values( 1, '<people><person name="bob"/></people>')
-- fails, no personsinsert xmltab values( 2, '<people><emp name="fred"/></people>')
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