2017-06-21 217 views
0

我一直試圖從SQL2008r2中寫入的擴展事件中獲取信息,試圖在SQL上創建一些更好的錯誤跟蹤形式。Xquery只返回一條記錄

經過大量的試驗和錯誤,我終於得到了一個工作,但它似乎只返回第一行。我試圖讀取信息到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記錄的其餘部分將通過拉?

任何幫助或建議將不勝感激。

回答

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...