1

SQL Server xml 列には、次のような xml があります。

<Test>  
    <Operations>
        <Operations type="OperationSend">
            <OperationSend>                             
                <ToCompanyId>1</ToCompanyId>
                <Date>2011-05-01T00:00:00</Date>                
            </OperationSend>
        </Operations>
        <Operations type="OperationSell">
            <OperationSell>
                <ToCompanyId>33</ToCompanyId>
                <Amount>12</Amount>
            </OperationSell>
        </Operations>
        <Operations type="OperationEdit">
            <OperationEdit>
                <ToCompanyId>12</ToCompanyId>
                <Date>2011-11-01T00:00:00</Date>    
            </OperationEdit>
        </Operations>
    </Operations>
</Test>

最後の操作 (12) から ToCompanyId を取得する必要があります。私はこのようなものに来ました。何を入れるべきですか??? ToCompanyId で任意の操作タイプが存在する可能性がある場合。

select testxml.query('(/Test/Operations/Operations)[last()]/???/ToCompanyId') from dbo.MyXmlTable
4

3 に答える 3

3

使用できます*

select testxml.query('(/Test/Operations/Operations)[last()]/*/ToCompanyId').value('.', 'int')
from MyXmlTable
于 2011-05-14T20:46:50.130 に答える
1

??? の代わりにnode()を入れてください

node()は、あらゆる種類のすべてのノードに一致します

于 2011-05-14T20:44:13.947 に答える
1

xml を @x という名前の変数に設定したと仮定すると、これは 12 を取得する方法です。

select  x.header.value('.', 'int') 
                from @x.nodes('//Test/Operations/Operations[last()]/OperationSend/ToCompanyId')
                 as x(header) 

テーブルの列から取得するクエリは少し異なりますが、XPATH は同じです。

select testxml.query('//Test/Operations/Operations[last()]/OperationSend/ToCompanyId') from dbo.MyXmlTable
于 2011-05-14T19:41:13.260 に答える