sp_detach_db 'testdb'
這就是 卸離(detach)資料庫 的標準語法,會讓 SQL Server 斷開資料庫與實體檔案的連結,但不會刪除實體 .mdf / .ndf / .ldf 檔案。
可搭配以下指令先切換單一使用者:
ALTER DATABASE testdb SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO
EXEC sp_detach_db 'testdb';
這樣可以強制中止其他連線,順利卸離。