SQL Server 删除数据库时,提示“无法删除数据库 "testdb",因为该数据库当前正在使用。”
USE master GO DECLARE @dbname sysname SET @dbname = 'testdb' --要删除的数据库名 DECLARE @s nvarchar(1000) DECLARE tb cursor LOCAL FOR SELECT s = 'kill ' + CAST(spid AS VARCHAR) FROM master.dbo.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 + ']')