Ads

19 November 2013

KILL Script - To Restore DB

Use Master
go

 declare @did int,   @dsql nvarchar(max) , @cnt int, @i int,@spid int
 Set @i=0

 select @did=dbid from sysdatabases where name = 'DBNAME HERE'
 select * into #sessions from sys.sysprocesses where dbid=@did and spid>50
 select @cnt=count(*) from #sessions

 while (@cnt>@i)

 begin
    select top 1 @spid= spid from #sessions where spid>50           
    print @spid
    set @dsql ='KILL ' + CAST(@SPId as nvarchar(10))
    delete from #sessions where spid=@spid
    EXEC sp_executeSQL @dSQL
    set @i=@i+1
 end

  drop table #sessions

================================================================
Note :- Use your DBName in the place of highlighted area
================================================================

No comments:

Post a Comment