Xquery只返回一条记录

时间:2017-06-21 11:59:24

标签: sql sql-server sql-server-2008-r2 xquery xquery-sql

我一直试图从我在SQL2008r2中编写的扩展事件中抓取信息,试图在SQL上创建一些更好的错误跟踪形式。

经过大量的试验和错误,我最终得到了一个工作,但它似乎只返回第一行。我试图将信息读入CTE,然后我可以对数据进行额外处理并以不同的格式返回

我写的是什么

;with events_cte as(
select
DATEADD(mi,
DATEDIFF(mi, GETUTCDATE(), CURRENT_TIMESTAMP),
xevents.event_data.value('(RingBufferTarget/event/@timestamp)[1]', 'datetime2')) AS [Timestamp],
xevents.event_data.value('(RingBufferTarget/event/data[@name="severity"]/value)[1]', 'bigint') AS [Error Severity],
xevents.event_data.value('(RingBufferTarget/event/data[@name="message"]/value)[1]', 'nvarchar(512)') AS [Error Message],
xevents.event_data.value('(RingBufferTarget/event/action[@name="sql_text"]/value)[1]', 'nvarchar(max)') AS [SQL Text],
xevents.event_data.value('(RingBufferTarget/event/action[@name="username"]/value)[1]', 'nvarchar(max)') AS [Username],
xevents.event_data.value('(RingBufferTarget/event/action[@name="database_id"]/value)[1]', 'nvarchar(max)') AS [DatabaseID]
From(Select CAST(xet.target_data AS xml) as event_data
FROM sys.dm_xe_session_targets xet
       INNER JOIN sys.dm_xe_sessions xe
              ON (xe.address = xet.event_session_address) 
WHERE xe.name = 'what_queries_are_failing') as xevents)

我正在使用的(示例)

    <RingBufferTarget eventsPerSec="0" processingTime="0" totalEventsProcessed="55" eventCount="55" droppedCount="0" memoryUsed="161803">
      <event name="error_reported" package="sqlserver" id="100" version="1" timestamp="2016-12-15T01:42:59.361Z">
        <data name="error">
          <type name="int32" package="package0" />
          <value>50000</value>
          <text />
        </data>
        <data name="severity">
          <type name="int32" package="package0" />
          <value>16</value>
          <text />
        </data>
        <data name="state">
          <type name="int32" package="package0" />
          <value>1</value>
          <text />
        </data>
        <data name="user_defined">
          <type name="boolean" package="package0" />
          <value>true</value>
          <text />
        </data>
        <data name="message">
          <type name="unicode_string" package="package0" />
          <value>User Defined Exception!!!</value>
          <text />
        </data>
        <action name="client_hostname" package="sqlserver">
          <type name="unicode_string" package="package0" />
          <value>hostname</value>
          <text />
        </action>
        <action name="database_id" package="sqlserver">
          <type name="uint16" package="package0" />
          <value>1</value>
          <text />
        </action>
        <action name="sql_text" package="sqlserver">
          <type name="unicode_string" package="package0" />
          <value>RAISERROR('User Defined Exception!!!', 16, 1) 
    </value>
          <text />
        </action>
        <action name="tsql_stack" package="sqlserver">
          <type name="unicode_string" package="package0" />
          <value>&lt;frame level='1' handle='0x010001001561AB09005F28B1000000000000000000000000' line='1' offsetStart='0' offsetEnd='-1'/&gt;</value>
          <text />
        </action>
        <action name="username" package="sqlserver">
          <type name="unicode_string" package="package0" />
          <value>user</value>
          <text />
        </action>
      </event>
      <event name="error_reported" package="sqlserver" id="100" version="1" timestamp="2016-12-15T01:42:59.361Z">
        <data name="error">
          <type name="int32" package="package0" />
          <value>50000</value>
          <text />
        </data>
        <data name="severity">
          <type name="int32" package="package0" />
          <value>16</value>
          <text />
        </data>
        <data name="state">
          <type name="int32" package="package0" />
          <value>1</value>
          <text />
        </data>
        <data name="user_defined">
          <type name="boolean" package="package0" />
          <value>true</value>
          <text />
        </data>
        <data name="message">
          <type name="unicode_string" package="package0" />
          <value>User Defined Exception!!!</value>
          <text />
        </data>
        <action name="client_hostname" package="sqlserver">
          <type name="unicode_string" package="package0" />
          <value>hostname</value>
          <text />
        </action>
        <action name="database_id" package="sqlserver">
          <type name="uint16" package="package0" />
          <value>1</value>
          <text />
        </action>
        <action name="sql_text" package="sqlserver">
          <type name="unicode_string" package="package0" />
          <value>RAISERROR('User Defined Exception!!!', 16, 1) 
    </value>
          <text />
        </action>
        <action name="tsql_stack" package="sqlserver">
          <type name="unicode_string" package="package0" />
          <value>&lt;frame level='1' handle='0x010001001561AB09005F28B1000000000000000000000000' line='1' offsetStart='0' offsetEnd='-1'/&gt;</value>
          <text />
        </action>
        <action name="username" package="sqlserver">
          <type name="unicode_string" package="package0" />
          <value>user</value>
          <text />
        </action>
      </event>
</RingBufferTarget>

我得到的结果:似乎只返回一行,可能是CTE吗?

所以我的问题是:

如何获取XML中的其余记录?

非常感谢任何帮助或建议。

1 个答案:

答案 0 :(得分:0)

您必须进行以下更改:

[1]

FROM sys.dm_xe_session_targets xet
       INNER JOIN sys.dm_xe_sessions xe ON (xe.address = xet.event_session_address) 
       OUTER APPLY xevents.event_data.nodes('RingBufferTarget/event') AS x(XmlCol)

[2]来自

xevents.event_data.value('(RingBufferTarget/event/@timestamp)[1]', 'datetime2')) AS [Timestamp],
xevents.event_data.value('(RingBufferTarget/event/data[@name="severity"]/value...

x.XmlCol.value('(@timestamp)[1]', 'datetime2')) AS [Timestamp],
x.XmlCol.value('(data[@name="severity"]/value...