Hungry DBA .com for DBAs who like food

This is useful in a SQL job where you want to restore a database on a nightly basis; thus need to ensure all sessions are killed beforehand.

This particular script kills all sessions in a database named DEMO

DECLARE @TmpWho TABLE
(spid INT, ecid INT, status NVARCHAR(max), loginame NVARCHAR(max),
hostname NVARCHAR(max), blk INT, dbname NVARCHAR(max), cmd NVARCHAR(max),
request_id INT)
INSERT INTO @TmpWho
EXEC sp_who
DECLARE @spid INT
DECLARE @tString VARCHAR(15)
DECLARE @getspid CURSOR
SET @getspid = CURSOR FOR
SELECT spid
FROM @TmpWho
WHERE dbname = 'DEMO' and status != 'background' OPEN @getspid
FETCH NEXT FROM @getspid INTO @spid
WHILE @@FETCH_STATUS = 0
BEGIN
SET @tString = 'KILL ' + CAST(@spid AS VARCHAR(5))
EXEC(@tString)
FETCH NEXT FROM @getspid INTO @spid
END
CLOSE @getspid
DEALLOCATE @getspid
GO