我正在尝试解析这个问题。我想为每个事件记录一条记录,所以我需要一种方法来分别查询每个事件。
但我希望它看起来像这样:
Event Name || ID || Timestamp || process_utilization
---------------------------------------------------------------------------------------------
scheduler_monitor_system_heal_ring_buffer_recorded || 0 || 0 || 33
这是XML:
<events>
<session startTime="2012-09-06T10:48:15.373" droppedEvents="0" largestDroppedEvent="0">
<RingBufferTarget truncated="0" processingTime="0" totalEventsProcessed="14" eventCount="14" droppedCount="0" memoryUsed="3994">
<event name="scheduler_monitor_system_health_ring_buffer_recorded" package="sqlos" timestamp="2012-09-19T16:46:33.091Z">
<data name="id">
<type name="uint32" package="package0" />
<value>0</value>
</data>
<data name="timestamp">
<type name="uint64" package="package0" />
<value>0</value>
</data>
<data name="process_utilization">
<type name="uint32" package="package0" />
<value>33</value>
</data>
</event>
<event name="resource_monitor_ring_buffer_recorded" package="sqlos" timestamp="2012-09-19T16:46:38.386Z">
<data name="id">
<type name="uint32" package="package0" />
<value>0</value>
</data>
</event>
</RingBufferTarget>
</session>
</events>
编辑: 或者,如果我们可以返回看起来像这样的行,那就是Just Fine:
scheduler_monitor_system_heal_ring_buffer_recorded || id || 0
scheduler_monitor_system_heal_ring_buffer_recorded || timestamp || 0
scheduler_monitor_system_heal_ring_buffer_recorded || process_utilization|| 33
编辑:(谢谢,马克) 使用Mark的代码,我得到的这是我想要的98% - 只需要事件名称。
SELECT
DataName = Evt.value('@name[1]', 'varchar(50)'),
TypeName= Evt.value('type[1]/@name[1]', 'varchar(50)'),
DataValue = Evt.value('value[1]', 'varchar(50)'),
DataText = Evt.value('text[1]', 'varchar(50)')
FROM
@input.nodes('/events/session/RingBufferTarget/event/data') as Tbl(Evt)
返回:
id uint32 0 NULL
timestamp uint64 0 NULL
process_utilization uint32 33 NULL
id uint32 0 NULL
(但显然需要事件名称)
答案 0 :(得分:2)
假设您在名为@input
的变量中包含XML,请尝试以下操作:
declare @input XML = '....(your XML here).....';
SELECT
EventName = Evt.value('(@name)[1]', 'varchar(50)'),
EventID = Evt.value('(data[@name="id"]/value)[1]', 'int'),
EventTimeStamp = Evt.value('(data[@name="timestamp"]/value)[1]', 'bigint'),
ProcessUtilization = Evt.value('(data[@name="process_utilization"]/value)[1]', 'int')
FROM
@input.nodes('/events/session/RingBufferTarget/event') as Tbl(Evt)
使用您的示例XML,我得到一个输出:
EventName EventID EventTimeStamp ProcessUtilization
scheduler_monitor_system_health_ring_buffer_record 0 0 33
resource_monitor_ring_buffer_recorded 0 NULL NULL
答案 1 :(得分:2)
非常感谢@Marc_S指出我正确的方向! Marc,如果你有某种方式可以获得信用,可以在这里或其他任何地方添加答案,我会相信你。
SELECT
DataName = Evt.value('../@name[1]', 'varchar(50)'),
DataName = Evt.value('@name[1]', 'varchar(50)'),
TypeName= Evt.value('type[1]/@name[1]', 'varchar(50)'),
DataValue = Evt.value('value[1]', 'varchar(50)'),
DataText = Evt.value('text[1]', 'varchar(50)')
FROM
@input.nodes('/events/session/RingBufferTarget/event/data') as Tbl(Evt)