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
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)
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)
;
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
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