Hi @amomen-8749,
Welcome to Microsoft Q&A!
from sys.dm_exec_query_stats q
full join sys.sysprocesses p on q.sql_handle = p.sql_handle
The sql_handle from sys.sysprocesses is binary(20) and represents the currently executing batch or object.
The sql_handle from sys.dm_exec_query_stats is varbinary(64) and is a token that uniquely identifies the batch or stored procedure that the query is part of.
The sql_handle, together with statement_start_offset and statement_end_offset, can be used to retrieve the SQL text of the query by calling the sys.dm_exec_sql_text dynamic management function
So it is not working if you would like to join these two tables with sql_handle.
Please refer below and check whether it is working:
select q.text [Script Text],
DB_NAME(t.dbid) as [Database],
program_name,
nt_username,
loginame,
last_batch last_execution_time
from sys.sysprocesses t
cross apply sys.dm_exec_sql_text (t.sql_handle) q
where spid <> @@spid
order by last_batch desc
Best regards
Melissa
If the answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.