我想从我的 xml 文档中的每个会话中获取所有读取节点及其值,以及从父节点获取会话 ID。我在 SQL Server 2008 实例上运行它。
我的xml设置是:
<Sessions>
<SessionID>99</SessionID>
<Readings>
<Reading>
<Point>
<Lat>-40.411558</Lat>
<Lng>175.63504</Lng>
</Point>
<Heading>54</Heading>
<Speed>0.1</Speed>
<Height>0</Height>
<FlowRate>0.69</FlowRate>
<AppRate>74</AppRate>
</Reading>
... more readings
</Readings>
<Sessions>
我最初的尝试(有效)如下所示,但是当我运行它时这似乎非常慢。但是,当我取出添加 SessionID 即 ../../SessionID 的行时,它似乎执行得更快。一个 12 秒的查询只需 2 秒。
SELECT
[SessionID] = c.value('(../../SessionID)[1]', 'int'),
[Heading] = c.value('(Heading)[1]', 'float'),
[Speed] = c.value('(Speed)[1]', 'float'),
[Height] = c.value('(Height)[1]', 'float'),
[FlowRate] = c.value('(FlowRate)[1]', 'float'),
[AppRate] = c.value('(AppRate)[1]', 'float'),
[Latitude] = c.value('(Point/Lat)[1]', 'float'),
[Longtitude] = c.value('(Point/Lng)[1]', 'float')
FROM
@XMLData.nodes('/Sessions/Readings/Reading') XMLData(c)
无论如何我可以做到这一点,而无需使用 ../../ 符号来引用 SessionID。我确实需要 SessionID,因为此查询用于将记录插入临时表:
insert into #MyTempTable
SELECT
[SessionID] = c.value('(../../SessionID)[1]', 'int'),
[Heading] = c.value('(Heading)[1]', 'float'),
[Speed] = c.value('(Speed)[1]', 'float'),
[Height] = c.value('(Height)[1]', 'float'),
[FlowRate] = c.value('(FlowRate)[1]', 'float'),
[AppRate] = c.value('(AppRate)[1]', 'float'),
[Latitude] = c.value('(Point/Lat)[1]', 'float'),
[Longtitude] = c.value('(Point/Lng)[1]', 'float')
FROM
@XMLData.nodes('/Sessions/Readings/Reading') XMLData(c)