1

以下のクエリを確認してください。

        declare @xmlRoot as xml
        set @xmlRoot= '<Root>
        <table1 col1="2012-03-02T16:42:55.777">
            <table2Array>
              <Table2 col2="abc">
              </Table2>  
              <Table2 col2="def"> 
              </Table2> 
            </table2Array>
          </table1>
         <table1 col1="2012-03-02T17:42:55.777">
            <table2Array>
              <Table2 col2="abc1">
              </Table2>  
              <Table2 col2="def1"> 
              </Table2> 
            </table2Array>
          </table1>
        </Root>'

        declare @a as varchar(1) 
           set @a= '1' 

         SELECT
        col1 =  item.value('./@col2', 'varchar(10)') 
        FROM @xmlRoot.nodes('Root/table1[1]/table2Array/Table2'  ) AS T(item);

-上記のクエリは期待される出力を返します

SELECT
col1 =  item.value('./@col2', 'varchar(10)') 
FROM @xmlRoot.nodes('Root/table1[*[local-name()=sql:variable("@a")]]/table2Array/Table2'  ) 
  AS T(item);

-上記のクエリは期待される出力を返しません

私はここで何を間違っているのですか?

親ノードに子ノードを識別するためのキー値がないため。インデックスを解析する必要があります。

4

1 に答える 1

1

これは私のために働いた:

DECLARE @a INT; -- data type is probably important!
SET @a = 1;

SELECT col1 =  item.value('./@col2', 'varchar(10)') 
FROM @xmlRoot.nodes('Root/table1[sql:variable("@a")]/table2Array/Table2') AS T(item);
于 2012-05-23T16:35:42.150 に答える