我真的不在乎我用這個測試數據庫做什麼......它是用於沙盒測試(連接到生產服務器實例的)!我想要做的就是殺死所有連接,如果不要求太多,則刪除並創建test_db ....並用一些測試數據恢復。ALTER DATABASE因數據庫'<db_name>'上無法鎖定而失敗。稍後再試
我試過USE [MASTER] RESTORE DATABASE test_DB WITH RECOVERY GO
,但得到這個錯誤:
Msg 3101, Level 16, State 1, Line 1 Exclusive access could not be obtained because the database is in use. Msg 3013, Level 16, State 1, Line 1 RESTORE DATABASE is terminating abnormally.
此外,試圖
USE [master] ALTER DATABASE test_DB SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
,並得到了錯誤:
Msg 5061, Level 16, State 1, Line 1 ALTER DATABASE failed because a lock could not be placed on database 'test_DB'. Try again later.
Msg 5069, Level 16, State 1, Line 1 ALTER DATABASE statement failed.
也沒
select min(spid) from master..sysprocesses where dbid = db_id('test_DB')
,但我的結果集返回NULL
以下是我的代碼:
--- Kill Connections
USE [master]
DECLARE @cmdKill VARCHAR(50)
DECLARE killCursor CURSOR FOR
SELECT 'KILL ' + Convert(VARCHAR(5), p.spid)
FROM master.dbo.sysprocesses AS p
WHERE p.dbid = db_id('test_DB')
OPEN killCursor
FETCH killCursor INTO @cmdKill
WHILE 0 = @@fetch_status
BEGIN
EXECUTE (@cmdKill)
FETCH killCursor INTO @cmdKill
END
CLOSE killCursor
DEALLOCATE killCursor
--Drop and Create
USE [master]
GO
IF EXISTS (SELECT name FROM sys.databases WHERE name = N'test_DB')
DROP DATABASE [test_DB]
GO
USE [master]
GO
CREATE DATABASE [test_DB] ON PRIMARY
(NAME = N'test_db_Data', FILENAME = N'\\some_place\d$\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\DATA\test_DB.mdf' , SIZE = 125635136KB , MAXSIZE = UNLIMITED, FILEGROWTH = 20%)
LOG ON
(NAME = N'test_db_Log', FILENAME = N'E:\SQLLogs\test_DB.ldf' , SIZE = 1064320KB , MAXSIZE = UNLIMITED, FILEGROWTH = 20%)
GO
ALTER DATABASE [test_db] SET ....
這就是問題....我沒有dbid = db_id('test_DB')。 – 2014-09-19 17:34:44
要獲得DBID的 的列表中選擇 \t 名 \t ,DBID FROM \t master.dbo.sysdatabases中 爲了通過 \t 名 – SQLburn 2014-09-19 19:38:47