How to kill database all users

USE master
Go

DECLARE @dbid INT , @spid INT , @STR NVARCHAR(500)
--Replace dbname with your database name
SET @dbid = DB_ID('dbname')
 
DECLARE rs SCROLL CURSOR FOR    
SELECT spid FROM sysprocesses WHERE dbid = @dbid   
OPEN rs   
FETCH FIRST FROM rs INTO @spid    
       WHILE @@fetch_status= 0   
       BEGIN   
              SELECT @STR = 'KILL '  + CONVERT ( VARCHAR(10) ,  @spid  )    
              EXEC sp_executesql @STR 
              FETCH NEXT FROM rs INTO @spid    
       END   
CLOSE rs   
DEALLOCATE rs  

Comments

Post a Comment

Hi User,
Thanks for visiting My Blog and please provide your valuable feedback and subscribe for more updates. Please don't post any spam content or comments.
Thank You

Popular Posts

Failed to execute the package or element. Build errors were encountered

Exception deserializing the package "The process cannot access the file because it is being used by another process."

Temporary enable and disable SSRS subscriptions