you can achieve the same using
sysprocesses system table[
^] which is even easier to use.
DECLARE @sql VARCHAR(MAX) = ''
;
WITH cte AS
(
SELECT spid
FROM sys.sysprocesses
WHERE spid > 50
AND DATEDIFF(HOUR, last_batch, GETDATE()) >= 4
)
SELECT @sql = @sql + 'KILL '+CAST(spid AS VARCHAR(10)) + ';' + CHAR(13)
FROM cte
PRINT @sql
This doesn't include the requirement #1 but that's just one additional IF.