gpt4 book ai didi

sql - 在扩展事件 session 环缓冲区中检索死锁时间戳

转载 作者:行者123 更新时间:2023-12-04 04:48:24 33 4
gpt4 key购买 nike

我正在尝试查询 system_health用于死锁事件的扩展事件环形缓冲区。到目前为止,我已经得到了如下所示的死锁图。对于缺乏引用,我深表歉意;我丢失了从中找到此查询的页面:

SELECT 
CAST(event_data.value('(event/data/value)[1]', 'nvarchar(max)') AS XML) AS DeadlockGraph
FROM
( SELECT XEvent.query('.') AS event_data
FROM
( -- Cast the target_data to XML
SELECT CAST(target_data AS XML) AS TargetData
FROM sys.dm_xe_session_targets AS st
INNER JOIN sys.dm_xe_sessions AS s
ON s.address = st.event_session_address
WHERE name = N'system_health'
AND target_name = N'ring_buffer'
) AS Data
-- Split out the Event Nodes
CROSS APPLY TargetData.nodes ('RingBufferTarget/event[@name="xml_deadlock_report"]') AS XEventData (XEvent)
) AS tab (event_data);

我还想为这些事件的时间戳添加一列。如果我运行内部查询,我会看到事件的时间戳在事件本身的 XML 中:
<event name="xml_deadlock_report"
package="sqlserver" id="123" version="1"
timestamp="2013-07-23T16:25:25.495Z">

如何解析此时间戳并显示它?我使用 TSQL 解析 XML 的经验有限。

我试过这个:
SELECT
CAST(event_data.value('/@timestamp', 'nvarchar(500)') AS datetime) AS [time],
CAST(event_data.value('(event/data/value)[1]', 'nvarchar(max)') AS XML) AS DeadlockGraph

但我收到错误:

Msg 2390, Level 16, State 1, Line 2 XQuery [tab.event_data.value()]: Top-level attribute nodes are not supported



这个:
CAST(event_data.value('(event/@timestamp)', 'nvarchar(500)') AS datetime) AS [time],

产生这个错误:

Msg 2389, Level 16, State 1, Line 2 XQuery [tab.event_data.value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'

最佳答案

改变这个

CAST(event_data.value('(event/@timestamp)', 'nvarchar(500)') AS datetime) AS [time],

有了这个
SELECT 
event_data.value('(/event/@timestamp)[1]','VARCHAR(50)') AS TS1,
event_data.value('(/event/@timestamp)[1]','DATETIMEOFFSET') AS TS2,
...

结果:
TS1                      TS2                               
------------------------ ----------------------------------
2013-07-23T17:49:46.072Z 2013-07-23 17:49:46.0720000 +00:00

关于sql - 在扩展事件 session 环缓冲区中检索死锁时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17816777/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com