How do I use extended events (SQL Server 2012) to tell me when certain tables are used in stored procedures. I want to drop some tables and so I want to know if the stored procedures the use those tables are actually being run.
The code sample sets up the supporting objects. And creates a session that I expect to work but doesn't. When you run those stored procedures (ListEmp
and ListProd
), I want them picked up because they contain the tables I am tracking (Employees
and Products
).
Note, I also tried using the sp_statement_starting
event:
-- 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;