Skip to main content David Edelstein's Blog

🦙
🦙

Get currently running SQL statement without VIEW SERVER STATE permission (SQL Server)

Published: 2015-06-05
dave@edelsteinautomotive.com
David Edelstein

Googling this first suggests that you find the current user using sp_who and then checking the results of:


SELECT  *
FROM sys.dm_exec_requests  
    	CROSS APPLY sys.dm_exec_sql_text(sql_handle)

If you do not hold the SQL server VIEW SERVER STATE permission, it will deny this query.

However, you can use this query to get the currently running SQL statement even without that permission!


dbcc inputbuffer(@@SPID) WITH NO_INFOMSGS 

This will return a result set that contains a column with the currently running query!

The WITH NO_INFOMSGS part is optional but will stop the statement from printing.