In SQL 2008 R2, how can you query the database for all spids that are open and from that get the last sql query they made?
1 Answer
The basic form is this using DMVs
SELECT
*
FROM
sys.dm_exec_requests er
CROSS APPLY
sys.dm_exec_sql_text(er.sql_handle) AS t
These can be refined or filtered as needed, for example like this link: https://sqlblog.org/2008/07/01/sys-dm_exec_requests