Monday, January 9, 2012

How to find out running process on a specified schema

SELECT p.SPID, Blocked_By = p.Blocked, p.Status, p.LogiName, p.HostName, Program = coalesce('Job: ' + j.name, p.program_name), DBName = db_name(p.dbid), Command = p.cmd, CPUTime = p.cpu, DiskIO = p.physical_io, LastBatch = p.Last_Batch, -- LastQuery = coalesce( (select [text] from sys.dm_exec_sql_text(p.sql_handle)), '' ), -- SQL Server 2005+ -- LastQuery = coalesce( (select * from ::fn_get_sql(p.sql_handle)), '' ), -- SQL Server 2000 ? FAILS p.WaitTime, p.LastWaitType, LoginTime = p.Login_Time, RunDate = GetDate(), [Server] = serverproperty('machinename'), [Duration(s)] = datediff(second, p.last_batch, getdate()) FROM master.dbo.sysprocesses p left outer join msdb.dbo.sysjobs j on substring(p.program_name,32,32) = substring(master.dbo.fn_varbintohexstr(j.job_id),3,100) where p.spid > 50 and p.status <> 'sleeping' and p.spid <> @@spid -- and ltrim(rtrim(p.loginame)) = 'NT AUTHORITY\SYSTEM' order by p.spid

No comments:

Post a Comment