Documente Academic
Documente Profesional
Documente Cultură
Using XML
What is XML?
XML is a plain-text, Unicode-based meta-language
Represents both structured and semi-structured data
Not tied to any programming language, OS, or vendor
SELECT
xmlCol.query(
Use exist
to check existence of value
'<InvoiceNumbers>
{
Bind
relational
columns and variables
for
$i in /InvoiceList/Invoice
return <InvoiceNo>
{number($i/@InvoiceNo)}
SELECT Invoices.query(
SELECT xmlCol.exist(
xmlCol.value(
</InvoiceNo>
'<Store>
'(/InvoiceList/Invoice/@InvoiceNo)[1]',
'/InvoiceList/Invoice[@InvoiceNo=1000]'
}
{sql:column("StoreName")}
)'int')
</InvoiceNumbers>')
</Store>')
SET xmlCol.modify(
@xmlDoc.modify(
SET
@xmlDoc.modify(
'insert
replace
element
value of
salesperson {"Bill"}
'delete
(/InvoiceList/Invoice/SalesPerson/text())[1]
as first
(/InvoiceList/Invoice/SalesPerson)[1]')
into "Ted"')
with
(/InvoiceList/Invoice)[1]')
nCol.value('@ProductID', 'int')
SELECT nCol.value('../@OrderID[1]',
'int')
Product,
ID,
nCol.value('@Quantity', 'int')
Qty
nCol.value('@ProductID[1]',
'int')
FROM
@xmlOrder.nodes('/Order/LineItem')
Prod
AS
nTable(nCol)
FROM
Orders
CROSS APPLY OrderDoc.nodes('/Order/LineItem')
AS nTable(nCol)
Converted to XML
Database Server
Client Application
SELECT
FROM
Status="5"
Status="5"
Status="5"
Status="5"
/>
/>
/>
/>
Tag
Parent
Invoice!1!InvoiceNo
Invoice!1!Date!Element
NULL
43659
2001-07-01T00:00:00
NULL
43660
2001-07-02T00:00:00
SELECT 11 AS
AS Tag,
Tag, NULL
NULL AS
AS Parent,
Parent,
SalesOrderID
SalesOrderID AS
AS [Invoice!1!InvoiceNo],
[Invoice!1!InvoiceNo],
OrderDate
OrderDate AS
AS [Invoice!1!Date!Element]
[Invoice!1!Date!Element]
FROM SalesOrderHeader
SalesOrderHeader
FOR XML EXPLICIT
Attribute
<Invoice InvoiceNo="43659">
<Date>2001-07-01T00:00:00</Date>
</Invoice>
<Invoice InvoiceNo="43660">...
Element
Use inner FOR XML with TYPE clause to return xml data type
SELECT 1 Cust.CustomerID,
CustomerType,
SalesOrderID, Status
Name
AS Tag,
CategoryName,
NULL AS Parent,
...
FROM SalesOrderHeader
Customer
Cust SubCategoryName
JOIN SalesOrderHeader [Order]
(SELECT Name
Cust.CustomerID
= [Order].CustomerID
UNION ALLON
FROM
ProductSubCategory
SubCategory
ORDER
Cust.CustomerID
SELECTBY
2 WHERE
AS Tag, 1SubCategory.ProductCategoryID
AS Parent, ...
=
FOR
AUTO,
FROMXML
SalesOrderDetail
Category.ProductCategoryID
OD JOIN ...
AUTO ELEMENTS
FOR XML EXPLICIT
FOR XML AUTO, TYPE, ELEMENTS)
FROM
ProductCategory Category
<Cust
<Cust>
CustomerID="1"
CustomerType="S">
FOR XML AUTO
<CustomerID>1</CustomerID>
<Order
SalesOrderID="43860" Status="5"/>
<Invoice
InvoiceNo="43659">
<CustomerType>S</CustomerType>
<Order SalesOrderID="44501" Status="5"/>
<Date>2001-07-01T00:00:00</Date>
<Order>
...
<Category
CategoryName="Accessories">
<LineItem
ProductID="709">Bike Socks, M</LineItem>
</Cust>
<SalesOrderID>43860</SalesOrderID>
<SubCategory>
<LineItem
ProductID="711">Helmet, Blue</LineItem>
<Status>5</Status>... Racks</SubCategoryName>
<SubCategoryName>Bike
</Invoice>...
</SubCategory>...
XML document
received from
client
Use sp_xml_removedocument
to clean up memory tree
Use OPENXML to
retrieve rowset
OPENXML Syntax
<Customer CustomerID="1" CustomerType="S">
<Order SalesOrderID="43860" Status="5"
OrderDate="2001-08-01T00:00:00">
<OrderDetail ProductID="761" Quantity="2"/>
<OrderDetail ProductID="770" Quantity="1"/>
</Order>
</Customer>
SELECT *
FROM OPENXML (@idoc, '/Customer/Order/OrderDetail', 1)
WITH (CustomerID int
'../../@CustomerID',
From
Customer
OrderID
int
'../@SalesOrderID',
element
OrderDate datetime '../@OrderDate',
ProdID
int
'@ProductID',
Quantity
int)
From Order
element
Defaults to Quantity attribute
What Is XQuery?
/InvoiceList/Invoice[@InvoiceNo=1000]
FLWOR statements
Statement
Description
For
where
order by
return
XQuery Basics
Sequences and QNames
Operators
Arithmetic comparison
General comparison
Value comparison
Node comparison
Logical
if-then-else
if ( $A eq $B )
then <result>A</result>
else <result>B</result>
Comments
(: Comment text :)
XQuery Expressions
Primary expressions
Literals
Variable references
Function calls
Path expressions
child::Address/child::Country
Sequence expressions
Relative
Absolute
/Address/Country
declare @x xml
set @x = '<root>
<abc></abc>
<abc attrAbc="1"></abc>
<abc attrAbc="2"></abc>
</root>'
SELECT
@x.query('/root/abc[attrAbc]')
Your XML values are relatively large and the retrieved parts
are relatively small
Notes
XML Schemas support wildcard sections
Logon information
Virtual machine
NYC-SQL-01
User name
Administrator
Password
P2ssw0rd
Lab Scenario
Adventure Works currently generates order manifests by performing a query
in the AdventureWorks database. The organization intends to deploy a new
Pocket PCbased application to its warehouse employees, which they will then
use when picking items from the warehouse for an order delivery. The itemchoosing application requires the order manifest to be downloaded as XML.
You must modify the existing Transact-SQL query that generates the order
manifest so that it retrieves the data as XML.
Adventure Works is also creating a new ordering system for specific customers
so that they can send orders as XML documents. This system is still in the
initial testing phase. You must create the appropriate Transact-SQL required
to store the XML order details in the existing database tables.
You must also write Transact-SQL code for a delivery scheduling application
that retrieves information from the XML data and modifies the information by
using xml methods.
Lab Review
What is the xml Data Type?
What does the FOR XML clause do?
What is the purpose of XML Schemas?
What is an XML Schema Collection?
What Is XQuery?
What does the query() method do?
What are XML Indexes?