2012-10-18 114 views
9

我有一些XML需要使用SQL Server 2008進行解析。我想我已經接近取得我想要的東西,但我沒有正確的語法(我相信)。在SQL Server中選擇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 

我在做什麼錯?

+1

+1容易測試/重複的例子, –

回答

6

的語法,抓住一個元素是:

SELECT Invoices.Node.value('(OrderId)[1]', 'VARCHAR(10)') 'OrderID' 
    , Invoices.Node.value('(Id)[1]', 'INT') 'InvoiceId' 
    , Items.Cd.value('.', 'VARCHAR(14)') 'ItemId' 
FROM 
    @doc.nodes('//InvoiceDetail') Invoices(Node) 
    CROSS APPLY Invoices.Node.nodes('./InvoiceItems/InvoiceItem/Cd') Items(Cd) 

這也似乎沒有明確的括號工作:

Invoices.Node.value('OrderId[1]', 'VARCHAR(10)') 

@語法的屬性,XQuery中沒有的元素。如果你有

<InvoiceDetail title="something"> 

然後你就可以查詢,使用:

SELECT Invoices.Node.value('@title', 'VARCHAR(MAX)') AS Title 
FROM @doc.nodes('//InvoiceDetail') Invoices(Node) 

下面是關於使用XQuery value

+0

感謝亞當的指導和外部鏈接。 – jared

0

下面的SQL也將與您的數據很好地工作的好文章:

-- Iterate through each of the "ROOT\InvoiceDetail" records in our XML 
SELECT 
    x.Rec.query('./OrderId').value('.', 'nvarchar(2000)') AS 'OrderID', 
    x.Rec.query('./Id').value('.', 'nvarchar(2000)') AS 'ID', 
    items.cd.query('./Cd').value('.', 'nvarchar(2000)') AS 'ItemID' 
FROM @doc.nodes('/ROOT/InvoiceDetail') as x(Rec) 
CROSS APPLY x.Rec.nodes('./InvoiceItems/InvoiceItem') Items(Cd) 

這給了我們這些結果:

enter image description here

(語法是莫名其妙,雖然,不是吧!)