drop database正确姿势

 你以为 drop database就可以跑路了吗?

USE master
GO
drop database BI_ABC;

报错: 无法删除数据库 "XXXX",因为该数据库当前正在使用

drop database正确姿势_第1张图片

正确姿势:

USE MASTER  
GO  
   
DECLARE @dbname SYSNAME  
SET @dbname = 'BI_ABC'    --BI_ABC是要删除的数据库库名  
   
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 + ']')  

 

你可能感兴趣的:(031-关系型数据库与SQL)