我备份了一个数据库:

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

当前回答

如果您想从备份文件恢复SQL Server数据库,可以使用以下脚本:

RESTORE DATABASE [MyDatabase] -- which database to restore
FROM DISK = N'X:\MyDatabase.bak' -- location of the database backup
WITH 
    FILE = 1, -- restore from a backup file
    -- declare where the file groups should be located (can be more than two)
    MOVE N'MyDatabase_Data' TO N'D:\SSDPATH\MyDatabase.mdf',
    MOVE N'MyDatabase_Log' TO N'E:\HDDPATH\MyDatabase.ldf',
    -- Tape option; only relevant if you backup from magnetic tape
    NOUNLOAD,
    -- brings the database online after the database got restored
    -- use this option when you don't want to restore incremental backups
    -- use NORECOVERY when you want to restore differential and incremental backup files
    RECOVERY,
    -- replace existing database with the backup 
    -- deletes the existing database
    REPLACE, 
    -- print log message for every 1 percent of restore
    STATS = 1;

其他回答

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

你试过运行验证吗?确保这是声音备份。

http://msdn.microsoft.com/en-us/library/ms188902.aspx

这个方法奏效了:

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.

我有一个。在我的数据库名称中,查询没有工作,因为(在'.'附近说不正确的语法)然后我意识到我需要一个括号的名称:

RESTORE DATABASE [My.DB.Name] WITH RECOVERY

为我解决问题的是

停止实例 在data文件夹中创建.mdf和.ldf文件的备份 重新启动实例 删除恢复卡住的数据库 把。mdf和。LDF文件返回到数据文件夹 将实例附加到.mdf和.ldf文件