gpt4 book ai didi

sql-server - 捕获SQL Server扩展 session 中传递的参数值?

转载 作者:行者123 更新时间:2023-12-03 22:07:32 25 4
gpt4 key购买 nike

我已经设置了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


到目前为止,一切都很好:



这对于跟踪在应用程序级别引发的错误的更多详细信息已经有很大帮助。

但是,使它变得更有用的是,如果我们可以看到传递给这些查询的参数值,就像在SQL Profiler中那样。所以当我们看到这样的错误时...

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参数的值是有帮助的。

我在哪里可以找到此信息的任何想法?

最佳答案

我认为只有sqlserver.rpc_completed事件会给您完整的SQL命令,包括参数值。

关于sql-server - 捕获SQL Server扩展 session 中传递的参数值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23658000/

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