SQL SERVER DBA, Linux and Azure: KILL all the connections for a database in SQL Server

KILL all the connections for a database in SQL Server

 ——- KILL all the connections for a database ——

Use Master

Go

Declare @dbname sysname

Set @dbname = 'Put-Your-DB-Name'

Declare @spid int

Select @spid = min(spid) from master.dbo.sysprocesses

where dbid = db_id(@dbname)

While @spid Is Not Null

Begin

Execute ('Kill' + @spid)

Select @spid = min(spid) from master.dbo.sysprocesses

where dbid = db_id(@dbname) and spid > @spid

End


No comments:

Post a Comment