• 1-888-289-2246
  • 24x7x365 Presence

How to kill Database Connections


If you see your database is making too many connections, then use the query below to kill all connections in one go.

=============

USE master
GO
DECLARE @dbname varchar(30), @spid varchar(10), @start datetime
SELECT @start = current_timestamp, @dbname = ‘airtel_sms’

— Timeout after 5 mts
while(exists(Select * FROM sysprocesses WHERE dbid = db_id(@dbname)) AND
datediff(mi, @start, current_timestamp) < 5)
begin
DECLARE spids CURSOR FOR
SELECT convert(varchar, spid) FROM sysprocesses
WHERE dbid = db_id(@dbname)
OPEN spids
while(1=1)
BEGIN
FETCH spids INTO @spid
IF @@fetch_status < 0 BREAK
exec(‘kill ‘ + @spid)
END
DEALLOCATE spids
END

]]>


Leave a Reply

Your email address will not be published. Required fields are marked *