我用来将此存储过程添加到主数据库,
改进:
- 修剪主机名,以便复制粘贴在 VNC 上工作。
- 添加了 LOCK 选项,仅用于查看当前锁定的进程。
用法:
- 执行 sp_who3 '活动'
- 执行 sp_who3 'LOCK'
- 执行 sp_who3 spid_No
而已。
CREATE procedure sp_who3
@loginame sysname = NULL --or 'active' or 'lock'
as
declare @spidlow int,
@spidhigh int,
@spid int,
@sid varbinary(85)
select @spidlow = 0
,@spidhigh = 32767
if @loginame is not NULL begin
if upper(@loginame) = 'ACTIVE' begin
select spid, ecid, status
, loginame=rtrim(loginame)
, hostname=rtrim(hostname)
, blk=convert(char(5),blocked)
, dbname = case
when dbid = 0 then null
when dbid <> 0 then db_name(dbid)
end
,cmd
from master.dbo.sysprocesses
where spid >= @spidlow and spid <= @spidhigh AND
upper(cmd) <> 'AWAITING COMMAND'
return (0)
end
if upper(@loginame) = 'LOCK' begin
select spid , ecid, status
, loginame=rtrim(loginame)
, hostname=rtrim(hostname)
, blk=convert(char(5),blocked)
, dbname = case
when dbid = 0 then null
when dbid <> 0 then db_name(dbid)
end
,cmd
from master.dbo.sysprocesses
where spid >= 0 and spid <= 32767 AND
upper(cmd) <> 'AWAITING COMMAND'
AND convert(char(5),blocked) > 0
return (0)
end
end
if (@loginame is not NULL
AND upper(@loginame) <> 'ACTIVE'
)
begin
if (@loginame like '[0-9]%') -- is a spid.
begin
select @spid = convert(int, @loginame)
select spid, ecid, status
, loginame=rtrim(loginame)
, hostname=rtrim(hostname)
, blk=convert(char(5),blocked)
, dbname = case
when dbid = 0 then null
when dbid <> 0 then db_name(dbid)
end
,cmd
from master.dbo.sysprocesses
where spid = @spid
end
else
begin
select @sid = suser_sid(@loginame)
if (@sid is null)
begin
raiserror(15007,-1,-1,@loginame)
return (1)
end
select spid, ecid, status
, loginame=rtrim(loginame)
, hostname=rtrim(hostname)
, blk=convert(char(5),blocked)
, dbname = case
when dbid = 0 then null
when dbid <> 0 then db_name(dbid)
end
,cmd
from master.dbo.sysprocesses
where sid = @sid
end
return (0)
end
/* loginame arg is null */
select spid,
ecid,
status
, loginame=rtrim(loginame)
, hostname=rtrim(hostname)
, blk=convert(char(5),blocked)
, dbname = case
when dbid = 0 then null
when dbid <> 0 then db_name(dbid)
end
,cmd
from master.dbo.sysprocesses
where spid >= @spidlow and spid <= @spidhigh
return (0) -- sp_who