34,873
社区成员
发帖
与我相关
我的任务
分享

DECLARE @hDoc int
EXEC sp_xml_preparedocument @hDoc OUTPUT,
N'<ROOT>
<Customers CustomerID="XYZAA" ContactName="Joe"
CompanyName="Company1">
<Orders CustomerID="XYZAA"
OrderDate="2000-08-25T00:00:00"/>
<Orders CustomerID="XYZAA"
OrderDate="2000-10-03T00:00:00"/>
</Customers>
<Customers CustomerID="XYZBB" ContactName="Steve"
CompanyName="Company2">No Orders yet!
</Customers>
</ROOT>'
-- Use OPENXML to provide rowset consisting of customer data.
INSERT Customers
SELECT *
FROM OPENXML(@hDoc, N'/ROOT/Customers')
WITH Customers
-- Use OPENXML to provide rowset consisting of order data.
INSERT Orders
SELECT *
FROM OPENXML(@hDoc, N'//Orders')
WITH Orders
-- Using OPENXML in a SELECT statement.
SELECT * FROM OPENXML(@hDoc, N'/ROOT/Customers/Orders') with (CustomerID nchar(5) '../@CustomerID', OrderDate datetime)
-- Remove the internal representation of the XML document.
EXEC sp_xml_removedocument @hDocPATH模式:
(1). SQL语句:
SELECT EmployeeID "@ID",FirstName "Name/FirstName",LastName "Name/LastName"
FROM Employees FOR XML PATH ('Employee')
(2). 所生成的XML文件
<Employee ID="1">
<Name>
<FirstName>NancyFirstName>
<LastName>DavolioLastName>
Name>
Employee>
<Employee ID="2">
<Name>
<FirstName>AndrewFirstName>
<LastName>FullerLastName>
Name>
Employee>
<Employee ID="3">
<Name>
<FirstName>JanetFirstName>
<LastName>LeverlingLastName>
Name>
Employee>
<Employee ID="4">
<Name>
<FirstName>MargaretFirstName>
<LastName>PeacockLastName>
Name>
Employee>
<Employee ID="5">
<Name>
<FirstName>StevenFirstName>
<LastName>BuchananLastName>
Name>
Employee>
<Employee ID="6">
<Name>
<FirstName>MichaelFirstName>
<LastName>SuyamaLastName>
Name>
Employee>
<Employee ID="7">
<Name>
<FirstName>RobertFirstName>
<LastName>KingLastName>
Name>
Employee>
<Employee ID="8">
<Name>
<FirstName>LauraFirstName>
<LastName>CallahanLastName>
Name>
Employee>