使用SQL

时间:2017-02-25 11:00:57

标签: sql sql-server xml database tsql

我有一个名为VehicleHistoryBlob的表,它具有以下结构:

VehicleHistoryBlobId int PRIMARY KEY
VehicleHistoryBlob XML

我需要编写SQL,查找VehicleHistoryBlob XML中的所有条目,其中Bus为父节点,Destination为子节点(Bus可以有多个Destinations,XML中的父节点并不总是Bus)。

<Bus>
...
    <Destination>
            <Name>The big building</Name>
            <DestinationCode> A21301423 </DestinationCode>
            <DestinationAddress> 440 Mountain View Parade </DestinationAddress>
            <DestinationCountry> USA </DestinationCountry>
    </Destination>
</Bus>'

我需要查询XML并查找所有将Bus作为父节点并将Destination作为子节点的条目,并将与XML关联的VehicleHistoryBlobId传递到我的临时表@tmpTable

DECLARE @tmpTable TABLE(theints INT)

我一直在尝试操作.nodes函数,但由于缺乏XML作为数据类型的经验,我很难得到准确的结果。

提前致谢!

1 个答案:

答案 0 :(得分:2)

要按XML列上的特定条件过滤行,您可以使用exist()方法而不是return empty($request->file('image')); 。例如,以下查询插入nodes() @temptable,其中对应的XML具有VehicleHistoryBlobId作为根元素和Bus子元素:

Destination

<强> sqlfiddle demo

相关问题