方法就是将使用此数据库的线程强制kill掉
USE master;
GO
DECLARE @dbname sysname;
SET @dbname = 'TingbugDatabase';--这个是要删除的数据库库名
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+']');
最后是删库,如果不需要的话可不执行
网友评论