cool hit counter Cannot delete the database because it is currently in use_Intefrankly

Cannot delete the database because it is currently in use


Copyright: This is an original post by the blogger and may not be reproduced without the blogger's permission. https://blog.csdn.net/huyuyang6688/article/details/43675371

This error was reported today when deleting a database in SQL Server, and I found a piece of code online that fixes the problem:.

Create a new query, execute the code below, perfect solution! (PS: Remember to change the "databasename" below to the name of the database to be deleted)

USE MASTER    
GO    
     
DECLARE @dbname SYSNAME    
SET @dbname = 'databasename' -- This is the name of the database library to be deleted    
     
DECLARE @s NVARCHAR(1000)    
DECLARE tb CURSOR LOCAL   
FOR  
    SELECT s = 'kill   ' + CAST(spid AS VARCHAR)  
    FROM   MASTER..sysprocesses  
    WHERE  dbid = DB_ID(@dbname)    
     
OPEN   tb      
FETCH   NEXT   FROM   tb   INTO   @s    
WHILE @@fetch_status = 0  
BEGIN  
    EXEC (@s)   
    FETCH NEXT FROM tb INTO @s  
END    
CLOSE   tb    
DEALLOCATE   tb    
  
EXEC ('drop   database   [' + @dbname + ']')    

Recommended>>
1、Large file downloads with progress bar display and MD5 checksum
2、Outpost Brain Map Five Clouds in the Sky GAIMS
3、DHCP Analysis of wareshark Network Protocol
4、Tossing a blog from scratch 4 migrating from Wordpress to Typecho
5、Basics are important 04 Table expressions next part

    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号