Technical Article

Kill Active connections

,

Simply Run the script in current database.

DECLARE @p_SPID int, @p_SQL nvarchar(2000),@dbName nvarchar(100)
SET @dbName = DB_NAME()
DECLARE #cur_Processes CURSOR FOR 
    SELECT p.SPID FROM master.dbo.sysprocesses AS p JOIN master.dbo.sysdatabases AS d ON( d.dbid = p.dbid ) 
    WHERE d.Name = @dbName AND p.SPID > 50 
    -- AND spid >= 51 (because spids of 50 or less are reserved for internal use.) 
OPEN #cur_Processes 
FETCH NEXT FROM #cur_Processes INTO @p_SPID 

WHILE @@FETCH_STATUS = 0 
BEGIN 
    SET @p_SQL = 'KILL ' + CONVERT( nvarchar(30), @p_SPID ) 
        PRINT @p_SQL 
        EXECUTE( @p_SQL ) 
    FETCH NEXT FROM #cur_Processes INTO @p_SPID 
END 
CLOSE #cur_Processes 
DEALLOCATE #cur_Processes

Rate

3 (5)

You rated this post out of 5. Change rating

Share

Share

Rate

3 (5)

You rated this post out of 5. Change rating