我有一些需要使用 SQL Server 2008 解析的 XML。我想我已经接近了我想要的,但我没有正确的语法(我相信)。
我有以下内容:
DECLARE @doc XML
SET @doc = '<ROOT>
<InvoiceDetail>
<OrderId>1000000</OrderId>
<OrderTypeId>2</OrderTypeId>
<Id>2000</Id>
<InvoiceItems>
<InvoiceItem>
<LineId>1</LineId>
<Cd>123456</Cd>
<Description>Item 1</Description>
<Quantity>1</Quantity>
<UnitPrice>99.990000</UnitPrice>
</InvoiceItem>
<InvoiceItem>
<LineId>2</LineId>
<Cd>234567</Cd>
<Description>Item 2</Description>
<Quantity>1</Quantity>
<UnitPrice>89.990000</UnitPrice>
</InvoiceItem>
</InvoiceItems>
</InvoiceDetail>
<InvoiceDetail>
<OrderId>1200000</OrderId>
<OrderTypeId>1</OrderTypeId>
<Id>3000</Id>
<InvoiceItems>
<InvoiceItem>
<LineId>1</LineId>
<Cd>234567</Cd>
<Description>Item 2</Description>
<Quantity>1</Quantity>
<UnitPrice>89.990000</UnitPrice>
</InvoiceItem>
<InvoiceItem>
<LineId>2</LineId>
<Cd>345678</Cd>
<Description>Item 3</Description>
<Quantity>1</Quantity>
<UnitPrice>79.990000</UnitPrice>
</InvoiceItem>
</InvoiceItems>
</InvoiceDetail>
</ROOT>'
SELECT
Invoices.Node.value('@OrderId', 'VARCHAR(10)') 'OrderID'
, Invoices.Node.value('@Id', 'INT') 'InvoiceId'
, Items.Cd.value('.', 'VARCHAR(14)') 'ItemId'
FROM
@doc.nodes('//InvoiceDetail') Invoices(Node)
CROSS APPLY Invoices.Node.nodes('./InvoiceItems/InvoiceItem/Cd') Items(Cd)
我得到以下结果:
NULL NULL 123456
NULL NULL 234567
NULL NULL 234567
NULL NULL 345678
我正在尝试获得以下信息:
1000000 2000 123456
1000000 2000 234567
1200000 3000 234567
1200000 3000 345678
我究竟做错了什么?