Tuesday, February 25, 2014

Kill All Process in database sql while restoring



If there is any problem that database currently in use and you are not able to restore database than you can run this query for block all processes currently running.

Use Master
Go

Declare @dbname sysname

Set @dbname = 'CDM_HVMS'

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
print 'done'