程序中的Ms Sql扩展事件过程

时间:2018-03-03 21:31:03

标签: sql-server tsql extended-events

我有一个程序,它执行另外两个程序,如:

ALTER PROCEDURE [tmp].[Test] (@SessionId INT)

BEGIN
    EXEC [tmp].[Test1] @SessionId = @SessionId

    EXEC [tmp].[Test2] @SessionId = @SessionId
END

然后,执行[tmp].[Test]程序,我想显示来自sys.fn_xe_file_target_read_file的信息,以便进行这些内部程序。

我创造了这样的东西:

SELECT event_name
    ,cpu_time
    ,duration
    ,logical_reads
    ,physical_reads
    ,writes
    ,statement
    ,plan_handle
    ,show_plan
    ,*
FROM (
    SELECT event_data.value('(event/@name)[1]', 'varchar(50)') AS event_name
        ,event_data.value('(event/action[@name="plan_handle"]/value)[1]', 'varchar(max)') AS plan_handle
        ,event_data.query('event/data[@name="showplan_xml"]/value/*') AS show_plan
        ,event_data.value('(event/data[@name="cpu_time"]/value)[1]', 'int') AS cpu_time
        ,event_data.value('(event/data[@name="duration"]/value)[1]', 'int') AS duration
        ,event_data.value('(event/data[@name="logical_reads"]/value)[1]', 'int') AS logical_reads
        ,event_data.value('(event/data[@name="physical_reads"]/value)[1]', 'int') AS physical_reads
        ,event_data.value('(event/data[@name="writes"]/value)[1]', 'int') AS writes
        ,event_data.value('(event/data[@name="statement"]/value)[1]', 'varchar(max)') AS statement
        ,event_data.value('(event/action[@name="sql_text"]/value)[1]', 'varchar(max)') AS sql_text
        ,*
    FROM (
        SELECT cast(event_data AS XML)
        FROM sys.fn_xe_file_target_read_file('E:\Pulpit\test_0_131645809565380000.xel', NULL, NULL, NULL)
        ) AS xevent(event_data)
    ) t

但它只返回[tmp].[Test]的结果。 如何获得[tmp].[Test2][tmp].[Test1]的结果?

1 个答案:

答案 0 :(得分:0)

SELECT cast(event_data AS XML)
FROM sys.fn_xe_file_target_read_file('E:\Pulpit\test_0_131645809565380000.xel', NULL, NULL, NULL)

UNION

SELECT cast(event_data AS XML)
FROM sys.fn_xe_file_target_read_file('E:\Pulpit\test_1_131645809565380000.xel', NULL, NULL, NULL)

UNION

SELECT cast(event_data AS XML)
FROM sys.fn_xe_file_target_read_file('E:\Pulpit\test_2_131645809565380000.xel', NULL, NULL, NULL)