如何在服务器跟踪期间查看在 sql 语句中使用的变量的值?

deu*_*uid 5 sql-server-2005 profiler

我有一个存储过程,其中包含带变量的 SQL 语句。例如,

update customer set cust_id = @newcust_id where issuedate < @transferdate and cust_id = @oldcust_id
Run Code Online (Sandbox Code Playgroud)

变量的值在 SP 中较早地分配。

如何在服务器跟踪期间显示这些变量的值?

Rem*_*anu 9

出于调试目的,您可以使用自定义事件通过sp_trace_generateevent以下方式将它们显式跟踪到 Profiler 中:

declare @tracedata varbinary(8000);
set @tracedata = cast(@transferdate as varbinary(8000));
exec sp_trace_generateevent 82, 
        N'@transferdate', 
        @tracedata );
set @tracedata = cast(@oldcust_id as varbinary(8000));
exec sp_trace_generateevent 82, 
        N'@oldcust_id', 
        @tracedata);
Run Code Online (Sandbox Code Playgroud)

您需要修改 Profiler 会话以监视用户User-Configurable Event Class,否则您将看不到生成的事件。sp_trace_generateevent调试完成后,我还会注释掉生产中的调用。该调用的开销并不大(特别是在没有监听监听的情况下),但仍应将其删除。

更新

exec调用不允许在参数列表中内联 CAST。我修改了代码以显示如何在 exec 之前进行转换。该值将在BinaryData列中,以十六进制表示(因此'daf'将显示为0x646166)。