2

我正在尝试解析这一点。我希望每个事件都有一个记录,所以我需要一种方法来分别查询每个事件。

但我希望它看起来像这样:

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>

编辑:或者,如果我们可以返回看起来像这样的行,那就太好了:

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

编辑:(谢谢,马克)使用马克的代码,我得到了我想要的 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

(但显然需要事件名称)

4

2 回答 2

2

非常感谢@Marc_S 为我指明了正确的方向!马克,如果有什么方法可以让您获得荣誉,请在此处添加答案或其他任何内容,我会感谢您。

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)
于 2012-09-20T18:35:53.093 回答
2

假设您的 XML 在名为 的变量中@input,请尝试以下操作:

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
于 2012-09-19T23:49:15.767 回答