如何使用扩展事件来跟踪存储过程中使用的表 运行
How to use extended events to track tables used in stored procedures being run
如何使用扩展事件 (SQL Server 2012) 告诉我何时在存储过程中使用了某些表。我想删除一些表,所以我想知道使用这些表的存储过程是否实际上是 运行。
代码示例设置了支持对象。并创建一个我希望工作但没有工作的会话。当您 运行 那些存储过程(ListEmp
和 ListProd
)时,我希望它们被拾取,因为它们包含我正在跟踪的表(Employees
和 Products
) .
请注意,我还尝试使用 sp_statement_starting
事件:
-- setup supporting objects
CREATE TABLE Employees (Col1 VARCHAR(10));
CREATE TABLE Products (Col1 VARCHAR(10));
GO
INSERT INTO Employees(Col1) VALUES ('Bob');
INSERT INTO Products(Col1) VALUES ('Bolts');
GO
CREATE PROCEDURE ListEmp
AS
SELECT * FROM Employees;
GO
CREATE PROCEDURE ListProd
AS
SELECT * FROM Products;
GO
-- create extended event (that is not doing what I want)
CREATE EVENT SESSION XE_TrackEmployeesAndProductsTables
ON SERVER
ADD EVENT sqlserver.sp_statement_completed
(
SET collect_statement=1
ACTION
(
sqlserver.database_name,
sqlserver.sql_text
)
WHERE
(
sqlserver.like_i_sql_unicode_string(sqlserver.sql_text,N'%Employees%')
OR sqlserver.like_i_sql_unicode_string(sqlserver.sql_text,N'%Products%')
)
);
ALTER EVENT SESSION XE_TrackEmployeesAndProductsTables ON SERVER STATE=START;
GO
-- run procs that I want to be picked up by my session (but aren't)
EXEC ListEmp;
EXEC ListProd;
-- cleanup
DROP EVENT SESSION XE_TrackEmployeesAndProductsTables ON SERVER;
DROP PROC ListEmp;
DROP PROC ListProd;
DROP TABLE Employees;
DROP TABLE Products;
我会将其添加到您要跟踪的任何存储过程的开头:
declare @msg nvarchar(128) = concat('Stored proc ',OBJECT_NAME(@@PROCID),' run.')
EXEC master..sp_trace_generateevent @event_class = 82, @userinfo = @msg;
您可以使用像这样的 XEvent 会话来跟踪:
CREATE EVENT SESSION [UserEvent] ON SERVER
ADD EVENT sqlserver.user_event
ADD TARGET package0.event_file(SET filename=N'UserEvent')
您可以像这样识别在静态 SQL 中引用 table 的所有过程:
select distinct object_name(d.object_id) referencing_object, SCHEMA_NAME(o.schema_id) referencing_object_schema
from sys.sql_dependencies d
join sys.objects o
on o.object_id = d.object_id
where d.referenced_major_id = object_id('Sales.SalesOrderHeader')
and o.type = 'P'
如何使用扩展事件 (SQL Server 2012) 告诉我何时在存储过程中使用了某些表。我想删除一些表,所以我想知道使用这些表的存储过程是否实际上是 运行。
代码示例设置了支持对象。并创建一个我希望工作但没有工作的会话。当您 运行 那些存储过程(ListEmp
和 ListProd
)时,我希望它们被拾取,因为它们包含我正在跟踪的表(Employees
和 Products
) .
请注意,我还尝试使用 sp_statement_starting
事件:
-- setup supporting objects
CREATE TABLE Employees (Col1 VARCHAR(10));
CREATE TABLE Products (Col1 VARCHAR(10));
GO
INSERT INTO Employees(Col1) VALUES ('Bob');
INSERT INTO Products(Col1) VALUES ('Bolts');
GO
CREATE PROCEDURE ListEmp
AS
SELECT * FROM Employees;
GO
CREATE PROCEDURE ListProd
AS
SELECT * FROM Products;
GO
-- create extended event (that is not doing what I want)
CREATE EVENT SESSION XE_TrackEmployeesAndProductsTables
ON SERVER
ADD EVENT sqlserver.sp_statement_completed
(
SET collect_statement=1
ACTION
(
sqlserver.database_name,
sqlserver.sql_text
)
WHERE
(
sqlserver.like_i_sql_unicode_string(sqlserver.sql_text,N'%Employees%')
OR sqlserver.like_i_sql_unicode_string(sqlserver.sql_text,N'%Products%')
)
);
ALTER EVENT SESSION XE_TrackEmployeesAndProductsTables ON SERVER STATE=START;
GO
-- run procs that I want to be picked up by my session (but aren't)
EXEC ListEmp;
EXEC ListProd;
-- cleanup
DROP EVENT SESSION XE_TrackEmployeesAndProductsTables ON SERVER;
DROP PROC ListEmp;
DROP PROC ListProd;
DROP TABLE Employees;
DROP TABLE Products;
我会将其添加到您要跟踪的任何存储过程的开头:
declare @msg nvarchar(128) = concat('Stored proc ',OBJECT_NAME(@@PROCID),' run.')
EXEC master..sp_trace_generateevent @event_class = 82, @userinfo = @msg;
您可以使用像这样的 XEvent 会话来跟踪:
CREATE EVENT SESSION [UserEvent] ON SERVER
ADD EVENT sqlserver.user_event
ADD TARGET package0.event_file(SET filename=N'UserEvent')
您可以像这样识别在静态 SQL 中引用 table 的所有过程:
select distinct object_name(d.object_id) referencing_object, SCHEMA_NAME(o.schema_id) referencing_object_schema
from sys.sql_dependencies d
join sys.objects o
on o.object_id = d.object_id
where d.referenced_major_id = object_id('Sales.SalesOrderHeader')
and o.type = 'P'