2010-11-02 23 views
6

Ben bir xml değişken @ResultDataSQL Server XML ad alanı Sorgulama Problemi

<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_> 

aşağıdaki var Ama çünkü düğümünde xmlns=... arasında ondan JournalNum değerlerini seçmek için görünmüyor olabilir. .Net'de onu almak için "{http://schemas.microsoft.com/dynamics/2006/02/documents/EntityKey}KeyData" gibi bir şey yapabilirim, ancak SQL'de bir sözdizimi hatası alıyorum.

Ben sadece bir geçici tabloya belge sırayla değer düğüm listesi, almak istiyorum ve bu da işe yaramaz ....

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

Düşünceler? Öneriler? Çözümler?

cevap

15

Anladım ... tabii ki, sağ yalnızca bir ad, her yerde önüne zorunda kalmamak için VARSAYILAN'ı kullanmış olabilir bu yana

;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

Çok teşekkür ederim! Sonunda, xpath sorgularımdaki bu sinir bozucu ad alanlarından kurtuldum. –

0

soran sonra:

;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) 

Ayrıca bazı adların birden fazla olduğu zamanki tüm boşlukları nasıl görmezden geldiğimi anladım ve hiçbir çarpışma yaşanmayacağınızı biliyorsunuz. Someone's blog.