在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
答案 0 :(得分:3)
您可以使用*
select testxml.query('(/Test/Operations/Operations)[last()]/*/ToCompanyId').value('.', 'int')
from MyXmlTable
答案 1 :(得分: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
答案 2 :(得分:1)
将 node()代替???
node()匹配任何类型的所有节点