有時候刪除庫的時候,會顯示沒法刪除數據庫,由於該數據庫當前正在使用。數據庫
EXEC msdb.dbo.sp_delete_database_backuphistory @database_name = N'DB_NAME' --填寫數據庫名稱,(msdb.dbo.sp_delete_database_backuphistory刪除數據庫備份和還原歷史記錄信息) GO USE [master] GO ALTER DATABASE [填數據庫名] SET SINGLE_USER WITH ROLLBACK IMMEDIATE --設置庫單用戶模式,和設置當即回滾 GO USE [master] GO DROP DATABASE [填數據庫名] GO
use master go declare @dbname sysname set @dbname = 'DB_NAME' --填寫沒法刪除的數據庫名稱 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 + ']')