我有一个很大的存储过程,我已经调用了许多其他的SP和函数,即使这样的内部SP也可能调用另一个SP。
所以我的问题是我可以用实际执行的方式跟踪所有这些SP的执行。
但是,我知道SQL Profiler跟踪sql语句和SP,但它并没有填充所有内部SP执行。
任何人都可以告诉我,我如何追踪所有这些内部SP的执行情况。
e.g:
CREATE PROCEDURE sp_test111
AS
BEGIN
SELECT * FROM Table1;
END
GO
CREATE PROCEDURE sp_test11
AS
BEGIN
EXEC sp_test111;
END
GO
CREATE PROCEDURE sp_test1
AS
BEGIN
EXEC sp_test11;
END
GO
CREATE PROCEDURE sp_test
AS
BEGIN
EXEC sp_test1;
END
GO
EXEC sp_test
--Expected result from SQL Trace
EXEC sp_test
EXEC sp_test1;
EXEC sp_test11;
EXEC sp_test111;
SELECT * FROM Table1;
答案 0 :(得分:0)
创建一个Profiler跟踪,包括SP:Starting和SP:StmtStarting事件类,以及您可能需要的任何其他过滤器。
如果需要无人参与运行跟踪,请将跟踪导出为服务器端跟踪的脚本(文件 - >导出 - >脚本跟踪定义)。自定义脚本中的sp_trace_create语句以指定所需的跟踪文件名和其他选项。见https://msdn.microsoft.com/en-us/library/ms190362(v=sql.100).aspx。下面是一个自定义跟踪脚本的示例(仅包括SP:为简洁起见)。
-- Create a Queue
declare @rc int
declare @TraceID int
declare @maxfilesize bigint
set @maxfilesize = 5
-- Please replace the text InsertFileNameHere, with an appropriate
-- filename prefixed by a path, e.g., c:\MyFolder\MyTrace. The .trc extension
-- will be appended to the filename automatically. If you are writing from
-- remote server to local drive, please use UNC path and make sure server has
-- write access to your network share
exec @rc = sp_trace_create @TraceID output, 2, N'C:\TraceFiles\StoredProcedureTrace', @maxfilesize, NULL, 2
if (@rc != 0) goto error
-- Client side File and Table cannot be scripted
-- Set the events
declare @on bit
set @on = 1
exec sp_trace_setevent @TraceID, 42, 1, @on
exec sp_trace_setevent @TraceID, 42, 9, @on
exec sp_trace_setevent @TraceID, 42, 2, @on
exec sp_trace_setevent @TraceID, 42, 66, @on
exec sp_trace_setevent @TraceID, 42, 3, @on
exec sp_trace_setevent @TraceID, 42, 4, @on
exec sp_trace_setevent @TraceID, 42, 5, @on
exec sp_trace_setevent @TraceID, 42, 6, @on
exec sp_trace_setevent @TraceID, 42, 7, @on
exec sp_trace_setevent @TraceID, 42, 8, @on
exec sp_trace_setevent @TraceID, 42, 10, @on
exec sp_trace_setevent @TraceID, 42, 11, @on
exec sp_trace_setevent @TraceID, 42, 12, @on
exec sp_trace_setevent @TraceID, 42, 14, @on
exec sp_trace_setevent @TraceID, 42, 22, @on
exec sp_trace_setevent @TraceID, 42, 26, @on
exec sp_trace_setevent @TraceID, 42, 28, @on
exec sp_trace_setevent @TraceID, 42, 29, @on
exec sp_trace_setevent @TraceID, 42, 34, @on
exec sp_trace_setevent @TraceID, 42, 35, @on
exec sp_trace_setevent @TraceID, 42, 41, @on
exec sp_trace_setevent @TraceID, 42, 49, @on
exec sp_trace_setevent @TraceID, 42, 50, @on
exec sp_trace_setevent @TraceID, 42, 51, @on
exec sp_trace_setevent @TraceID, 42, 60, @on
exec sp_trace_setevent @TraceID, 42, 62, @on
exec sp_trace_setevent @TraceID, 42, 64, @on
-- Set the Filters
declare @intfilter int
declare @bigintfilter bigint
exec sp_trace_setfilter @TraceID, 10, 0, 7, N'SQL Server Profiler - 4cd51131-ed78-47fc-aad5-d3d12c6409d4'
-- Set the trace status to start
exec sp_trace_setstatus @TraceID, 1
-- display trace id for future references
select TraceID=@TraceID
goto finish
error:
select ErrorCode=@rc
finish:
go
记下脚本返回的跟踪ID,以便稍后停止和删除跟踪。以下是使用跟踪ID 2的示例。
EXEC sp_trace_setstatus 2, 0; --stop
EXEC sp_trace_setstatus 2, 2; --delete
您可以在Profiler中查看跟踪文件,也可以使用fn_trace_getdata(https://msdn.microsoft.com/en-us/library/ms188425(v=sql.100).aspx)查看跟踪文件。