今天恢复一个SQLServer2008R2,发现问题,然后通过园友的文章解决了问题,特记录备用
原文地址:http://www.cnblogs.com/adandelion/archive/2006/09/30/519534.html
今天在sqlServer20005 的management studio里使用bak文件还原数据库的时候,总是失败!
Restore failed for Server 'ADANDELI'. (Microsoft.SqlServer.Smo)
An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo
The backup set holds a backup of a database other than the existing 'AAA' database.
RESTORE DATABASE is terminating abnormally. (Microsoft SQL Server,错误: 3154)
找不到原因(已经选择了"overwrite the existing database"),只能使用命令来还原了,不知道为什么会这样.
--返回由备份集内包含的数据库和日志文件列表组成的结果集。
--主要获得逻辑文件名
USE master
RESTORE FILELISTONLY
FROM DISK = 'g:ack.Bak'
Go
************************************************
/*
利用bak恢复数据库,强制还原(REPLACE)
STATS = 10 每完成10%显示一条记录
DBTest和DBTest_log是上面g:ack.Bak里的逻辑文件
*/
USE master
RESTORE DATABASE DB
FROM DISK = 'g:ack.Bak'
WITH MOVE 'DBTest' TO 'E:Program FilesMicrosoft SQL Server2005DataDB.mdf',
MOVE 'DBTest_log' TO 'E:Program FilesMicrosoft SQL Server2005DataDB_log.ldf',
STATS = 10, REPLACE
GO
++++++++++++++++++++++++++++++++
/*
备份数据DB 到.bak文件。然后利用此bak文件恢复一个新的数据库DBTest。
*/
USE master
BACKUP DATABASE DB
TO DISK = 'g:DBBack0930.bak'
RESTORE FILELISTONLY
FROM DISK = 'g:DBBack0930.bak'
RESTORE DATABASE DBTest
FROM DISK = 'g:DBBack0930.bak'
WITH MOVE 'DBTest' TO 'E:Program FilesMicrosoft SQL Server2005DataDBTest.mdf',
MOVE 'DBTest_log' TO 'E:Program FilesMicrosoft SQL Server2005DataDBTest_log.ldf'
GO
---******************************
--主要获得逻辑文件名
USE master
RESTORE FILELISTONLY
FROM DISK = 'g:ack.Bak'
Go
************************************************
/*
利用bak恢复数据库,强制还原(REPLACE)
STATS = 10 每完成10%显示一条记录
DBTest和DBTest_log是上面g:ack.Bak里的逻辑文件
*/
USE master
RESTORE DATABASE DB
FROM DISK = 'g:ack.Bak'
WITH MOVE 'DBTest' TO 'E:Program FilesMicrosoft SQL Server2005DataDB.mdf',
MOVE 'DBTest_log' TO 'E:Program FilesMicrosoft SQL Server2005DataDB_log.ldf',
STATS = 10, REPLACE
GO
++++++++++++++++++++++++++++++++
/*
备份数据DB 到.bak文件。然后利用此bak文件恢复一个新的数据库DBTest。
*/
USE master
BACKUP DATABASE DB
TO DISK = 'g:DBBack0930.bak'
RESTORE FILELISTONLY
FROM DISK = 'g:DBBack0930.bak'
RESTORE DATABASE DBTest
FROM DISK = 'g:DBBack0930.bak'
WITH MOVE 'DBTest' TO 'E:Program FilesMicrosoft SQL Server2005DataDBTest.mdf',
MOVE 'DBTest_log' TO 'E:Program FilesMicrosoft SQL Server2005DataDBTest_log.ldf'
GO
---******************************
SQL Server 还原数据库出现的“ 因为数据库正在使用 无法获得独占访问权 .....”
终极解决办法
1)ALTER DATABASE [DB] SET OFFLINE WITH ROLLBACK IMMEDIATE
2)执行还原操作
3)ALTER database [DB] set online
2)执行还原操作
3)ALTER database [DB] set online