Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capture passed parameter values in SQL Server extended sessions?

I've set up SQL Server extended events to catch the last 1000 failed queries in a specific database into a ring buffer:

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

I can now query these errors like so:

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

So far, so good:

screenshot

This has already been a big help in tracking down more detail on errors that get thrown at the application level.

What would make it even more useful, though, would be if we could see the parameter values being passed to these queries, the way you can in SQL Profiler. So when we see an error like this...

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

for this SQL...

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

... it would be informative to know what the values of the @NewValue and @KeyValue parameters were.

Any ideas where I might find this information?

like image 976
Herb Caudill Avatar asked May 14 '14 14:05

Herb Caudill


People also ask

Which target is allowed in extended events?

etw_classic_sync_target target SQL Server Extended Events can inter-operate with Event Tracing for Windows (ETW) to monitor system activity. For more information, see: Event Tracing for Windows Target. Monitor System Activity Using Extended Events.

Can we pass parameter in SQL query?

To pass a parameter to the database, we have to separate it from the parameters in order to avoid the formula firewall. This means that we break query folding. And this means that Power Query needs to pull in all 40 million records, and process them.

What is extended event session in SQL Server?

Extended Events is a lightweight performance monitoring system that enables users to collect data needed to monitor and troubleshoot problems in SQL Server.


1 Answers

I think only sqlserver.rpc_completed event will give you complete SQL command including parameter values.

like image 137
Endrju Avatar answered Oct 14 '22 21:10

Endrju