2010-11-02 40 views
6

我有一個xml變量@ResultDataSQL Server的XML命名空間查詢問題

<EntityKey_x005B__x005D_> 
    <EntityKey> 
    <KeyData xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey"> 
     <KeyField> 
     <Field>JournalNum</Field> 
     <Value>LJRN000071</Value> 
     </KeyField> 
    </KeyData> 
    </EntityKey> 
    <EntityKey> 
    <KeyData xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey"> 
     <KeyField> 
     <Field>JournalNum</Field> 
     <Value>LJRN000072</Value> 
     </KeyField> 
    </KeyData> 
    </EntityKey> 
    <EntityKey> 
    <KeyData xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey"> 
     <KeyField> 
     <Field>JournalNum</Field> 
     <Value>LJRN000073</Value> 
     </KeyField> 
    </KeyData> 
    </EntityKey> 
    <EntityKey> 
    <KeyData xmlns="http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey"> 
     <KeyField> 
     <Field>JournalNum</Field> 
     <Value>LJRN000074</Value> 
     </KeyField> 
    </KeyData> 
    </EntityKey> 
</EntityKey_x005B__x005D_> 

以下,但我似乎無法選擇它,因爲xmlns=...的節點上的JournalNum值。在.Net中,我可以像"{http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey}KeyData"那樣來檢索它,但是我在SQL中出現語法錯誤。

我只是想獲得超值的節點列表,文檔順序到一個臨時表,這也不管用....

SELECT IDENTITY(int,1,1) as 'ID', 
    c.query('(KeyData/KeyField/Value)[1]') as 'JournalNum' 
INTO #tmpBatches 
FROM @ResultData.nodes('//EntityKey') t(c) 

的思考?建議?解決方案?

回答

15

明白了......當然,問

;WITH XMLNAMESPACES (N'http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey' as DYN) 
    SELECT IDENTITY(int,1,1) 
       as 'ID', 
      c.value('(DYN:KeyData/DYN:KeyField/DYN:Value)[1]', 'VARCHAR(40)') 
       as 'JournalNum' 
    INTO #tmpBatches 
    FROM @ResultData.nodes('//EntityKey') t(c) 
+0

非常感謝你!我終於在我的xpath查詢中擺脫了這些煩人的命名空間。 – 2013-09-23 14:44:45

0

因爲你只有一個命名空間,你也可以使用默認值,以避免前綴無處不之後:

;WITH XMLNAMESPACES (DEFAULT N'http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey') 
     SELECT IDENTITY(int,1,1)             
     as 'ID', c.value('(<strike>DYN:</strike>KeyData/DYN:KeyField/DYN:Value)[1]', 'VARCHAR(40)') 
     as 'JournalNum' 
      INTO #tmpBatches 
     FROM @ResultData.nodes('//EntityKey') t(c) 

而且,一些筆記我偶然發現如何忽略所有命名空間,當有多個命名空間時,你知道你將沒有衝突。 Someone's blog.