patternsqlModerate
Capture SQL Server queries without third-party tooling and without using deprecated features?
Viewed 0 times
withoutthirdsqlpartyandcapturedeprecatedfeaturesusingtooling
Problem
Is it possible to capture all queries sent to an MS SQL Server, without third-party tooling and without using deprecated features?
I'm looking for something similar to the "general query log" in MySQL.
Here's an example using a third-party tool:
Here's an alternative using deprecated features:
Is there a non-deprecated, native solution?
I'm looking for something similar to the "general query log" in MySQL.
Here's an example using a third-party tool:
- https://blog.devart.com/capturing-sql-server-trace-data.html
Here's an alternative using deprecated features:
- https://learn.microsoft.com/en-us/sql/tools/sql-server-profiler/sql-server-profiler?view=sql-server-2017
Is there a non-deprecated, native solution?
Solution
You could use Extended Events to capture this data. However, depending on how much traffic your server gets this could become a lot of data very quickly and could result in performance issues.
I would look at limiting the events that you track to something like
From SSMS, you would go to Management > Extended Events > Session and either use the
Here is an example of something I implemented recently to capture queries on a specific server, you can add filters to remove queries that you don't want to see - like some server names or application names that execute queries as well:
This captures the details of what has been executed and puts it into a file for easy querying and analysis.
I would look at limiting the events that you track to something like
sqlserver.rpc_completed or sqlserver.sql_statement_completed, these only capture what was completed. Erin Stellato wrote a great piece on useing XEvent Profiler to capture queries in SQL Server. From SSMS, you would go to Management > Extended Events > Session and either use the
New Session Wizard or New Session to start building your session to track data. The Microsoft Docs go into a lot of detail on how to set this up.Here is an example of something I implemented recently to capture queries on a specific server, you can add filters to remove queries that you don't want to see - like some server names or application names that execute queries as well:
CREATE EVENT SESSION [Track Queries] ON SERVER
ADD EVENT sqlserver.rpc_completed(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_name,sqlserver.nt_username,sqlserver.server_instance_name,sqlserver.server_principal_name,sqlserver.sql_text,sqlserver.username)
WHERE (NOT [sqlserver].[like_i_sql_unicode_string]([sqlserver].[client_hostname],N'%name%')
AND [sqlserver].[not_equal_i_sql_unicode_string]([sqlserver].[client_hostname],N'name')
AND NOT [sqlserver].[like_i_sql_unicode_string]([sqlserver].[client_app_name],N'%name%')
AND [sqlserver].[server_principal_name]<>N'')),
ADD EVENT sqlserver.sql_statement_completed(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_name,sqlserver.nt_username,sqlserver.server_instance_name,sqlserver.server_principal_name,sqlserver.sql_text,sqlserver.username)
WHERE (NOT [sqlserver].[like_i_sql_unicode_string]([sqlserver].[client_hostname],N'%%')
AND [sqlserver].[not_equal_i_sql_unicode_string]([sqlserver].[client_hostname],N'name')
AND NOT [sqlserver].[like_i_sql_unicode_string]([sqlserver].[client_app_name],N'%name%')
AND [sqlserver].[server_principal_name]<>N''))
ADD TARGET package0.event_file(SET filename=N'D:\XE\TrackQueries.xel',max_file_size=(5120))
WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=OFF,STARTUP_STATE=OFF)
GOThis captures the details of what has been executed and puts it into a file for easy querying and analysis.
Code Snippets
CREATE EVENT SESSION [Track Queries] ON SERVER
ADD EVENT sqlserver.rpc_completed(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_name,sqlserver.nt_username,sqlserver.server_instance_name,sqlserver.server_principal_name,sqlserver.sql_text,sqlserver.username)
WHERE (NOT [sqlserver].[like_i_sql_unicode_string]([sqlserver].[client_hostname],N'%name%')
AND [sqlserver].[not_equal_i_sql_unicode_string]([sqlserver].[client_hostname],N'name')
AND NOT [sqlserver].[like_i_sql_unicode_string]([sqlserver].[client_app_name],N'%name%')
AND [sqlserver].[server_principal_name]<>N'<username>')),
ADD EVENT sqlserver.sql_statement_completed(
ACTION(sqlserver.client_app_name,sqlserver.client_hostname,sqlserver.database_name,sqlserver.nt_username,sqlserver.server_instance_name,sqlserver.server_principal_name,sqlserver.sql_text,sqlserver.username)
WHERE (NOT [sqlserver].[like_i_sql_unicode_string]([sqlserver].[client_hostname],N'%<name>%')
AND [sqlserver].[not_equal_i_sql_unicode_string]([sqlserver].[client_hostname],N'name')
AND NOT [sqlserver].[like_i_sql_unicode_string]([sqlserver].[client_app_name],N'%name%')
AND [sqlserver].[server_principal_name]<>N'<username>'))
ADD TARGET package0.event_file(SET filename=N'D:\XE\TrackQueries.xel',max_file_size=(5120))
WITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=OFF,STARTUP_STATE=OFF)
GOContext
StackExchange Database Administrators Q#243484, answer score: 13
Revisions (0)
No revisions yet.