今天在SQL Server中删除数据库的时候,报的这个错误,在网上找到一段代码,可以解决这个问题:
新建查询,执行下面的代码,完美解决!(PS:记得把下面的“databasename”改成要删除的数据库名)
USE MASTER GO DECLARE @dbname SYSNAME SET @dbname = 'databasename' --这个是要删除的数据库库名 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 + ']')