森栏
这将向您显示在SQL 2000或SQL 2005服务器上运行时间最长的SPID:select P.spid, right(convert(varchar, dateadd(ms, datediff(ms, P.last_batch, getdate()), '1900-01-01'), 121), 12) as 'batch_duration', P.program_name, P.hostname, P.loginamefrom master.dbo.sysprocesses Pwhere P.spid > 50and P.status not in ('background', 'sleeping')and P.cmd not in ('AWAITING COMMAND' ,'MIRROR HANDLER' ,'LAZY WRITER' ,'CHECKPOINT SLEEP' ,'RA MANAGER')order by batch_duration desc如果需要从结果中查看给定spid的SQL运行,请使用以下命令:declare @spid int, @stmt_start int, @stmt_end int, @sql_handle binary(20)set @spid = XXX -- Fill this inselect top 1 @sql_handle = sql_handle, @stmt_start = case stmt_start when 0 then 0 else stmt_start / 2 end, @stmt_end = case stmt_end when -1 then -1 else stmt_end / 2 endfrom sys.sysprocesseswhere spid = @spidorder by ecidSELECT SUBSTRING( text, COALESCE(NULLIF(@stmt_start, 0), 1), CASE @stmt_end WHEN -1 THEN DATALENGTH(text) ELSE (@stmt_end - @stmt_start) END )FROM ::fn_get_sql(@sql_handle)