SQL Server – Identifying Current Running Queries with Database Name

Use the script below to get the current running queries on your server.

SELECT 
sqltext.TEXT
,DB_Name(req.database_id) as DatabaseName
,sess.last_request_start_time 
,req.total_elapsed_time/1000 as total_elapsed_time
,sess.session_id AS SessionID 
,req.blocking_session_id AS BlockedBy
,req.command
,req.cpu_time AS CPU_Time 
,sess.HOST_NAME as RunningFrom 
,sess.login_name 
,wait_type
,wait_time
,sess.status 
,req.reads 
,req.writes 
,req.logical_reads 
,sess.program_name
FROM 
sys.dm_exec_requests req 
INNER JOIN sys.dm_exec_sessions sess ON sess.session_id = req.session_id      
AND sess.is_user_process = 1 
CROSS APPLY 
sys.dm_exec_sql_text(sql_handle) AS sqltext
WHERE DB_Name(req.database_id) NOT IN ('master','tempdb')
ORDER BY total_elapsed_time DESC

 

Knowledge worth sharing...Share on linkedin
Linkedin
Share on facebook
Facebook
Share on google
Google
Share on twitter
Twitter