Knowing the SPID of the current session we are using. select @@SPID To know the database name from the dbid select DB_Name (DBID) and viceversa, select db_ID (DBname) We can get the query being executed on any session using the below query. DECLARE @Handle varbinary(64); SELECT @Handle = sql_handle FROM sys.sysprocesses WHERE spid = 62; SELECT text FROM ::fn_get_sql(@Handle)