SQL Server:过滤sp_who2的输出

在SQL Server下,是否有一种简单的方法来过滤sp_who2的输出?例如,假设我只想显示某个数据库的行。

200725 次浏览

你可以试试

DECLARE @Table TABLE(
SPID INT,
Status VARCHAR(MAX),
LOGIN VARCHAR(MAX),
HostName VARCHAR(MAX),
BlkBy VARCHAR(MAX),
DBName VARCHAR(MAX),
Command VARCHAR(MAX),
CPUTime INT,
DiskIO INT,
LastBatch VARCHAR(MAX),
ProgramName VARCHAR(MAX),
SPID_1 INT,
REQUESTID INT
)


INSERT INTO @Table EXEC sp_who2


SELECT  *
FROM    @Table
WHERE ....

过滤你需要的东西。

一种方法是创建一个临时表:

CREATE TABLE #sp_who2
(
SPID INT,
Status VARCHAR(1000) NULL,
Login SYSNAME NULL,
HostName SYSNAME NULL,
BlkBy SYSNAME NULL,
DBName SYSNAME NULL,
Command VARCHAR(1000) NULL,
CPUTime INT NULL,
DiskIO INT NULL,
LastBatch VARCHAR(1000) NULL,
ProgramName VARCHAR(1000) NULL,
SPID2 INT
)
GO


INSERT INTO #sp_who2
EXEC sp_who2
GO


SELECT *
FROM #sp_who2
WHERE Login = 'bla'
GO


DROP TABLE #sp_who2
GO

是的,通过将sp_who2的输出捕获到表中,然后从表中进行选择,但这是一种糟糕的方式。首先,尽管sp_who2很流行,但它是一个没有文档的过程,您不应该依赖于没有文档的过程。其次,所有sp_who2可以做的事情,以及更多的事情,都可以从sys.dm_exec_requests和其他dmv中获得,并且show可以被过滤、排序、连接和所有其他随可查询行集而来的好东西。

有相当多好的sp_who3用户存储过程——我相信Adam Machanic做了一个非常好的存储过程,AFAIK。

亚当称之为谁是活跃的: http://whoisactive.com < / p >

基于http://web.archive.org/web/20080218124946/http://sqlserver2005.databases.aspfaq.com/how-do-i-mimic-sp-who2.html

我已经创建了以下脚本,
,它解决了使用DMV查找任何数据库的活动连接,该工作在sql 2005, 2008和2008R2

下面的脚本使用sys.dm_exec_sessionssys.dm_exec_requestssys.dm_exec_connectionssys.dm_tran_locks

Declare @dbName varchar(1000)
set @dbName='abc'


;WITH DBConn(SPID,[Status],[Login],HostName,DBName,Command,LastBatch,ProgramName)
As
(
SELECT
SPID = s.session_id,
Status = UPPER(COALESCE
(
r.status,
ot.task_state,
s.status,
'')),
[Login] = s.login_name,
HostName = COALESCE
(
s.[host_name],
'  .'
),
DBName = COALESCE
(
DB_NAME(COALESCE
(
r.database_id,
t.database_id
)),
''
),
Command = COALESCE
(
r.Command,
r.wait_type,
wt.wait_type,
r.last_wait_type,
''
),
LastBatch = COALESCE
(
r.start_time,
s.last_request_start_time
),
ProgramName = COALESCE
(
s.program_name,
''
)
FROM
sys.dm_exec_sessions s
LEFT OUTER JOIN
sys.dm_exec_requests r
ON
s.session_id = r.session_id
LEFT OUTER JOIN
sys.dm_exec_connections c
ON
s.session_id = c.session_id
LEFT OUTER JOIN
(
SELECT
request_session_id,
database_id = MAX(resource_database_id)
FROM
sys.dm_tran_locks
GROUP BY
request_session_id
) t
ON
s.session_id = t.request_session_id
LEFT OUTER JOIN
sys.dm_os_waiting_tasks wt
ON
s.session_id = wt.session_id
LEFT OUTER JOIN
sys.dm_os_tasks ot
ON
s.session_id = ot.session_id
LEFT OUTER JOIN
(
SELECT
ot.session_id,
CPU_Time = MAX(usermode_time)
FROM
sys.dm_os_tasks ot
INNER JOIN
sys.dm_os_workers ow
ON
ot.worker_address = ow.worker_address
INNER JOIN
sys.dm_os_threads oth
ON
ow.thread_address = oth.thread_address
GROUP BY
ot.session_id
) tt
ON
s.session_id = tt.session_id
WHERE
COALESCE
(
r.command,
r.wait_type,
wt.wait_type,
r.last_wait_type,
'a'
) >= COALESCE
(
'',
'a'
)
)


Select * from DBConn
where DBName like '%'+@dbName+'%'

Astander的回答略有改进。我喜欢把我的标准放在最上面,让它更容易日复一日地重用:

DECLARE @Spid INT, @Status VARCHAR(MAX), @Login VARCHAR(MAX), @HostName VARCHAR(MAX), @BlkBy VARCHAR(MAX), @DBName VARCHAR(MAX), @Command VARCHAR(MAX), @CPUTime INT, @DiskIO INT, @LastBatch VARCHAR(MAX), @ProgramName VARCHAR(MAX), @SPID_1 INT, @REQUESTID INT


--SET @SPID = 10
--SET @Status = 'BACKGROUND'
--SET @LOGIN = 'sa'
--SET @HostName = 'MSSQL-1'
--SET @BlkBy = 0
--SET @DBName = 'master'
--SET @Command = 'SELECT INTO'
--SET @CPUTime = 1000
--SET @DiskIO = 1000
--SET @LastBatch = '10/24 10:00:00'
--SET @ProgramName = 'Microsoft SQL Server Management Studio - Query'
--SET @SPID_1 = 10
--SET @REQUESTID = 0


SET NOCOUNT ON
DECLARE @Table TABLE(
SPID INT,
Status VARCHAR(MAX),
LOGIN VARCHAR(MAX),
HostName VARCHAR(MAX),
BlkBy VARCHAR(MAX),
DBName VARCHAR(MAX),
Command VARCHAR(MAX),
CPUTime INT,
DiskIO INT,
LastBatch VARCHAR(MAX),
ProgramName VARCHAR(MAX),
SPID_1 INT,
REQUESTID INT
)
INSERT INTO @Table EXEC sp_who2
SET NOCOUNT OFF
SELECT  *
FROM    @Table
WHERE
(@Spid IS NULL OR SPID = @Spid)
AND (@Status IS NULL OR Status = @Status)
AND (@Login IS NULL OR Login = @Login)
AND (@HostName IS NULL OR HostName = @HostName)
AND (@BlkBy IS NULL OR BlkBy = @BlkBy)
AND (@DBName IS NULL OR DBName = @DBName)
AND (@Command IS NULL OR Command = @Command)
AND (@CPUTime IS NULL OR CPUTime >= @CPUTime)
AND (@DiskIO IS NULL OR DiskIO >= @DiskIO)
AND (@LastBatch IS NULL OR LastBatch >= @LastBatch)
AND (@ProgramName IS NULL OR ProgramName = @ProgramName)
AND (@SPID_1 IS NULL OR SPID_1 = @SPID_1)
AND (@REQUESTID IS NULL OR REQUESTID = @REQUESTID)

一个非常简单的方法是在EXCEL中创建一个ODBC链接,并从那里运行SP_WHO2。

你可以随时刷新,因为它是EXCEL,一切都可以很容易地操纵!

可以 将结果保存到临时表中,但它会更好地直接去master.dbo.sysprocesses上的源。

下面是一个查询,它将返回与sp_who2几乎完全相同的结果:

SELECT  spid,
sp.[status],
loginame [Login],
hostname,
blocked BlkBy,
sd.name DBName,
cmd Command,
cpu CPUTime,
physical_io DiskIO,
last_batch LastBatch,
[program_name] ProgramName
FROM master.dbo.sysprocesses sp
JOIN master.dbo.sysdatabases sd ON sp.dbid = sd.dbid
ORDER BY spid

现在你可以很容易地添加任何ORDER BYWHERE子句来获得有意义的输出。


或者,你也可以考虑在SSMS中使用< >强活动监视器< / >强 (Ctrl + Alt + 一个)

类似于KyleMit回答,可以直接选择SP_WHO2使用的表,尽管我认为它只需要dbo。sysprocesses表。

如果有人打开这个SP,它可以理解它是做什么的。这是我的最佳选择,以获得与SP_WHO2类似的输出

select convert(char(5),sp.spid) as SPID
,  CASE lower(sp.status)
When 'sleeping' Then lower(sp.status)
Else  upper(sp.status)
END as Status
, convert(sysname, rtrim(sp.loginame)) as LOGIN
, CASE sp.hostname
When Null  Then '  .'
When ' ' Then '  .'
Else    rtrim(sp.hostname)
END as HostName
, CASE isnull(convert(char(5),sp.blocked),'0')
When '0' Then '  .'
Else isnull(convert(char(5),sp.blocked),'0')
END as BlkBy
, case when sp.dbid = 0 then null when sp.dbid <> 0 then db_name(sp.dbid) end as DBName
, sp.cmd as Command
, sp.cpu as CPUTime
, sp.physical_io as DiskIO
, sp.last_batch as LastBatch
, sp.program_name as ProgramName
from master.dbo.sysprocesses sp (nolock)
;

在这个选择中,您可以选择您需要的字段,并有您想要的顺序。

这是你的解决方案: http://blogs.technet.com/b/wardpond/archive/2005/08/01/the-openrowset-trick-accessing-stored-procedure-output-in-a-select-statement.aspx < / p >
select * from openrowset ('SQLOLEDB', '192.168.x.x\DATA'; 'user'; 'password', 'sp_who')

第一个也是最好的答案的延伸…我已经在主数据库上创建了一个存储过程,然后可以将参数传递给..例如数据库的名称:

USE master
GO


CREATE PROCEDURE sp_who_db
(
@sDBName varchar(200)   = null,
@sStatus varchar(200)   = null,
@sCommand varchar(200)  = null,
@nCPUTime int           = null
)
AS
DECLARE @Table TABLE
(
SPID INT,
Status VARCHAR(MAX),
LOGIN VARCHAR(MAX),
HostName VARCHAR(MAX),
BlkBy VARCHAR(MAX),
DBName VARCHAR(MAX),
Command VARCHAR(MAX),
CPUTime INT,
DiskIO INT,
LastBatch VARCHAR(MAX),
ProgramName VARCHAR(MAX),
SPID_1 INT,
REQUESTID INT
)


INSERT INTO @Table EXEC sp_who2


SELECT  *
FROM    @Table
WHERE   (@sDBName IS NULL OR DBName = @sDBName)
AND     (@sStatus IS NULL OR Status = @sStatus)
AND     (@sCommand IS NULL OR Command = @sCommand)
AND     (@nCPUTime IS NULL OR CPUTime > @nCPUTime)
GO

我可以扩展它,添加一个按参数排序的参数,甚至是一个kill参数,这样它就会终止到特定数据的所有连接

我写在这里是为了将来我自己使用。它使用sp_who2并插入到表变量中,而不是temp表,因为如果不删除temp表,它就不能使用两次。 并显示阻塞和阻塞程序在同一行

--blocked: waiting becaused blocked by blocker
--blocker: caused blocking
declare @sp_who2 table(
SPID int,
Status varchar(max),
Login varchar(max),
HostName varchar(max),
BlkBy varchar(max),
DBName varchar(max),
Command varchar(max),
CPUTime int,
DiskIO int,
LastBatch varchar(max),
ProgramName varchar(max),
SPID_2 int,
REQUESTID int
)
insert into @sp_who2 exec sp_who2
select  w.SPID blocked_spid, w.BlkBy blocker_spid, tblocked.text blocked_text, tblocker.text blocker_text
from    @sp_who2 w
inner join sys.sysprocesses pblocked on w.SPID = pblocked.spid
cross apply sys.dm_exec_sql_text(pblocked.sql_handle) tblocked
inner join sys.sysprocesses pblocker on case when w.BlkBy = '  .' then 0 else cast(w.BlkBy as int) end = pblocker.spid
cross apply sys.dm_exec_sql_text(pblocker.sql_handle) tblocker
where   pblocked.Status = 'SUSPENDED'

我做了改进,不仅得到了被阻塞的进程,还得到了阻塞的进程:

DECLARE @Table TABLE
(
SPID INT, Status VARCHAR(MAX), LOGIN VARCHAR(MAX), HostName VARCHAR(MAX), BlkBy VARCHAR(MAX), DBName VARCHAR(MAX), Command VARCHAR(MAX), CPUTime INT, DiskIO INT, LastBatch VARCHAR(MAX), ProgramName VARCHAR(MAX), SPID_1 INT, REQUESTID INT
)


INSERT INTO @Table EXEC sp_who2


SELECT  *
FROM    @Table
WHERE
BlkBy not like '  .'
or
SPID in (SELECT  BlkBy from @Table where BlkBy not like '  .')


delete from @Table