我备份了一个数据库:

BACKUP DATABASE MyDatabase
TO DISK = 'MyDatabase.bak'
WITH INIT --overwrite existing

然后试图恢复它:

RESTORE DATABASE MyDatabase
   FROM DISK = 'MyDatabase.bak'
   WITH REPLACE --force restore over specified database

现在数据库处于还原状态。

有些人推测,这是因为备份中没有日志文件,需要使用以下方法前滚:

RESTORE DATABASE MyDatabase
WITH RECOVERY 

当然,这是行不通的:

Msg 4333, Level 16, State 1, Line 1
The database cannot be recovered because the log was not restored.
Msg 3013, Level 16, State 1, Line 1
RESTORE DATABASE is terminating abnormally.

在灾难性的情况下,你想要的是一个无法工作的恢复。


备份包含数据文件和日志文件:

RESTORE FILELISTONLY 
FROM DISK = 'MyDatabase.bak'

Logical Name    PhysicalName
=============   ===============
MyDatabase    C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\MyDatabase.mdf
MyDatabase_log  C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\DATA\MyDatabase_log.LDF

当前回答

这个方法奏效了:

http://social.msdn.microsoft.com/Forums/en/sqldatabaseengine/thread/8dd1b91d-3e14-4486-abe6-e3a550bfe457

我有一个情况,我的数据库显示恢复状态,我不能运行任何查询,不能与我们的软件连接。

为了摆脱这种情况,我所做的是:

停止windows服务中的所有SQL相关服务。 我打开了数据文件夹,其中Ldf和Mdf文件驻留在SQL目录中,通常是这样的: “C: \程序文件 ***********\ 该软件\数据 然后我复制了数据库的Ldf和Mdf文件: (数据库名称)。MDF和[db name]_log.ldf

我把这两个文件都复制到另一个文件夹。

Then I started all the SQL related services (in step 1) again from windows services. Started my MS SQL Management studio with normal login. Right click on the culprit database and hit DELETE (to delete the database at all). All the LDF and MDF files related to this database have gone from DATA folder (mentioned in step 2). Created a new database with the same name (same name of the one I deleted in step 6 - the culprit database). Then [database name]->right click -> tasks -> Take Offline. I then Copied both the files (from step 3) back to the DATA folder (step 2). [database name]->right click -> tasks -> Bring Online.

其他回答

I had a similar issue with restoring using SQL Management Studio. I tried to restore a backup of the database to a new one with a different name. At first this failed and after fixing the new database's file names it was successfully performed - in any case the issue I'm describing re-occurred even if I got this right from the first time. So, after the restoration, the original database remained with a (Restoring...) next to its name. Considering the answers of the forum above (Bhusan's) I tried running in the query editor on the side the following:

RESTORE DATABASE "[NAME_OF_DATABASE_STUCK_IN_RESTORING_STATE]"

这解决了问题。一开始我遇到了麻烦,因为数据库名称包含特殊字符。我通过在周围添加双引号来解决这个问题-单引号将不起作用,给出“错误的语法接近……”错误。

这是我尝试解决这个问题(数据库处于恢复状态)的最小解决方案,我希望它可以应用到更多的情况。

在我的情况下,我只是右键单击数据库,然后任务—>恢复—>数据库—>Ok,一切都变得很好。

您需要使用WITH RECOVERY选项和数据库RESTORE命令使数据库在线,作为恢复过程的一部分。

当然,这仅适用于不打算恢复任何事务日志备份的情况,即只希望恢复数据库备份,然后能够访问数据库。

你的命令应该是这样的,

RESTORE DATABASE MyDatabase
   FROM DISK = 'MyDatabase.bak'
   WITH REPLACE,RECOVERY

使用SQL Server Management Studio中的恢复数据库向导可能会更成功。通过这种方式,您可以选择特定的文件位置、覆盖选项和WITH恢复选项。

RESTORE DATABASE {DatabaseName}
   FROM DISK = '{databasename}.bak'
   WITH REPLACE, RECOVERY

在我的例子中,使用SQL命令删除挂起状态为“还原…”的数据库就足够了

 drop database <dbname> 

在查询窗口中。

然后我右键单击Databases并选择Refresh,这将删除SQL Server Management Studio (SSMS)中的条目。之后,我做了一个新的恢复,工作良好(注意,使它脱机不起作用,重新启动SQL服务不起作用,服务器重新启动也不起作用)。