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

21
2.2 SQL Server 2005 的 XML 的的的的

Transcript of 2.2 SQL Server 2005 的 XML 支援功能. Overview XML Enhancements in SQL Server 2005 The xml Data...

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

2.2 SQL Server 2005 的 XML 支援功能

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

Overview

XML Enhancements in SQL Server 2005

The xml Data Type

Using XQuery

Page 3: 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

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

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.

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

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

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

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.

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

Practice: Using the OPENXML Function

In this practice, you will execute the OPENXML function

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

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

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

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

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

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))

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

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

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

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

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

<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

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

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

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

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

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

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

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

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

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

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

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

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

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

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>')

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

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