Slight improvement to Astander's answer. I like to put my criteria at top, and make it easier to reuse day to day:
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)