Find last query executed by specific session on SQL Server

SQL Server enables you to check last query executed by each session. Queries in this article will show you how to do it.

Find a session

First you need to identify session. Find out more here:

exec sp_who

Result

Result shows three sessions from host LS-MICHALWROBEL:

sample results

Find last query

Now we will use session ID (spid) to find last query issued in this session:

dbcc inputbuffer(<session ID>)

Columns

  • EventType - event type. RPC Event means procedure call. Language Event means sql query.
  • Parameters - number of query parameters.
  • EventInfo - last statement in session. Returns only first 4000 characters.

Sample results

We can see that last query issued insession 67 was "select * from mytable"

sample results