捕获SQL Server扩展会话中传递的参数值?

时间:2014-05-14 14:46:53

标签: sql-server extended-events

我已设置SQL Server扩展事件,以将特定数据库中的最后1000个失败查询捕获到环形缓冲区中:

Create Event Session [Errors] on Server
Add Event sqlserver.error_reported (
    Action(
        sqlos.task_time,
        sqlserver.sql_text
    )
    Where sqlserver.database_name=N'MyDatabase'
    And error_number<>5701 -- Ignore changed db context messages
)
Add target package0.ring_buffer(Set max_memory=102400)
With (
    MAX_MEMORY=4096 KB,
    EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,
    MAX_DISPATCH_LATENCY=30 SECONDS,
    MAX_EVENT_SIZE=0 KB,
    MEMORY_PARTITION_MODE=NONE,
    TRACK_CAUSALITY=OFF,
    STARTUP_STATE=ON
)
Go

我现在可以像这样查询这些错误:

Select 
    xmldata = Cast(xet.target_data as xml) 
Into #rbd
From sys.dm_xe_session_targets xet 
Join sys.dm_xe_sessions xe ON (xe.address = xet.event_session_address)
Where xe.name='errors' and target_name='ring_buffer'

; With Errors as (
    Select 
        e.query('.').value('(/event/@timestamp)[1]', 'datetime') as "TimeStamp",
        e.query('.').value('(/event/data[@name="message"]/value)[1]', 'nvarchar(max)') as "Message",
        e.query('.').value('(/event/action[@name="sql_text"]/value)[1]', 'nvarchar(max)') as "SqlText",
        e.query('.').value('(/event/action[@name="task_time"]/value)[1]', 'bigint')/@@TIMETICKS  as "Duration"
    From #rbd
    Cross Apply XMLData.nodes('/RingBufferTarget/event') as Event(e)
)
Select * from Errors
Where SqlText is not null

Drop Table #rbd

到目前为止,非常好:

screenshot

这对于追踪在应用程序级别引发的错误的更多细节已经有了很大的帮助。

但是,如果我们能够在SQL事件探查器中看到参数值传递给这些查询,那将会更有用。所以当我们看到这样的错误时......

Conversion failed when converting date and/or time from character string.   

这个SQL ...

(@NewValue nvarchar(10),@KeyValue int)
Update SomeTable 
Set SomeField=@NewValue Where SomeTableID=@KeyValue

...知道@NewValue@KeyValue参数的值是什么有用。

我可能会在哪些想法中找到这些信息?

1 个答案:

答案 0 :(得分:4)

我认为只有sqlserver.rpc_completed事件才能为您提供包含参数值的完整SQL命令。