Saturday, 14 February 2015

SQL SERVER – Cursor to Kill All Process in Database

When you run the script please make sure that you run it in different database then the one you want all the processes to be killed.
———————————————————————-CREATE TABLE #TmpWho(spid INTecid INTstatus VARCHAR(150), loginameVARCHAR(150),hostname VARCHAR(150), blk INTdbname VARCHAR(150), cmdVARCHAR(150))INSERT INTO #TmpWhoEXEC sp_whoDECLARE @spid INT
DECLARE 
@tString VARCHAR(15)DECLARE @getspid CURSOR
SET 
@getspid =   CURSOR FOR
SELECT 
spidFROM #TmpWhoWHERE dbname 'mydb'OPEN @getspidFETCH NEXT FROM @getspid INTO @spidWHILE @@FETCH_STATUS 0BEGIN
SET 
@tString 'KILL ' CAST(@spid AS VARCHAR(5))EXEC(@tString)FETCH NEXT FROM @getspid INTO @spidEND
CLOSE 
@getspidDEALLOCATE @getspidDROP TABLE #TmpWhoGO
———————————————————————–

No comments:

Post a Comment