You're probably doing something wrong (like executing in a different database than you're filtering on). But to answer the question:
- A batch is a set (one or more) of commands that a client sends to SQL Server.
GO
is a batch separator setting of the SSMS client (it's not a TSQL command).
In your example, you're sending two batches. One is the create proc
and the other dynamically executes the proc.
Repro
- Create this XE for tracking
/* Clean up previous version */
IF EXISTS (SELECT 1 FROM sys.server_event_sessions ses WHERE
ses.name = 'BatchStatementModuleSp'
)
BEGIN
DROP EVENT SESSION BatchStatementModuleSp ON SERVER;
END;
GO
CREATE EVENT SESSION [BatchStatementModuleSp] ON SERVER
ADD EVENT sqlserver.module_end(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.sql_text,sqlserver.tsql_stack, sqlserver.database_id)
WHERE ([sqlserver].[client_app_name]=N'SingleSessionTracking')),
ADD EVENT sqlserver.module_start(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.sql_text,sqlserver.tsql_stack, sqlserver.database_id)
WHERE ([sqlserver].[client_app_name]=N'SingleSessionTracking')),
ADD EVENT sqlserver.sp_statement_completed(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.sql_text,sqlserver.tsql_stack, sqlserver.database_id)
WHERE ([sqlserver].[client_app_name]=N'SingleSessionTracking')),
ADD EVENT sqlserver.sp_statement_starting(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.sql_text,sqlserver.tsql_stack, sqlserver.database_id)
WHERE ([sqlserver].[client_app_name]=N'SingleSessionTracking')),
ADD EVENT sqlserver.sql_batch_completed(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.sql_text,sqlserver.tsql_stack, sqlserver.database_id)
WHERE ([sqlserver].[client_app_name]=N'SingleSessionTracking')),
ADD EVENT sqlserver.sql_batch_starting(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.sql_text,sqlserver.tsql_stack, sqlserver.database_id)
WHERE ([sqlserver].[client_app_name]=N'SingleSessionTracking')),
ADD EVENT sqlserver.sql_statement_completed(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.sql_text,sqlserver.tsql_stack, sqlserver.database_id)
WHERE ([sqlserver].[client_app_name]=N'SingleSessionTracking')),
ADD EVENT sqlserver.sql_statement_starting(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.sql_text,sqlserver.tsql_stack, sqlserver.database_id)
WHERE ([sqlserver].[client_app_name]=N'SingleSessionTracking'))
WITH (TRACK_CAUSALITY = ON);
GO
IF NOT EXISTS (SELECT 1 FROM sys.dm_xe_sessions dxs WHERE dxs.name = 'BatchStatementModuleSp')
BEGIN
ALTER EVENT SESSION [BatchStatementModuleSp] ON SERVER STATE = START
END
- Open a new connection and add an additional connection property like this
Application Name=SingleSessionTracking;
- Run your script in the session from step 2.
CREATE OR ALTER PROCEDURE [TestThis]
AS
BEGIN
SELECT 'Test';
END
GO
EXEC (N'EXEC TestThis');
And you'll see something like this
And here's an edited version that showcases the relations and nesting