2

我正在尝试查询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

但我得到了错误:

消息 2390,级别 16,状态 1,行 2 XQuery [tab.event_data.value()]:不支持顶级属性节点

这个:

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

产生此错误:

消息 2389,级别 16,状态 1,第 2 行 XQuery [tab.event_data.value()]:'value()' 需要单例(或空序列),找到类型为 'xdt:untypedAtomic *' 的操作数

4

1 回答 1

3

改变这个

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
于 2013-07-23T17:29:48.830 回答