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

Restore of database 'DataBase_Name' failed. (Microsoft.SqlServer.Management.RelationalEngineTasks)

Cannot convert "Column" between a unicode and a non-unicode string data types in SSIS