代码之家  ›  专栏  ›  技术社区  ›  CaffGeek

SQL Server Xml命名空间查询问题

  •  6
  • CaffGeek  · 技术社区  · 14 年前

    我在xml变量中有以下内容 @ResultData

    <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=... 在节点上。在.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)
    

    思想?建议?解决?

    2 回复  |  直到 14 年前
        1
  •  15
  •   CaffGeek    14 年前

    明白了…当然,问了之后

        ;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)
    
        2
  •  0
  •   Jay Wheeler    7 年前

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