简体   繁体   English

检查数据库是否处于恢复状态

[英]Checking If Database In Restoring State

I'm running a T-SQL script that drops a database and then restores it. 我正在运行一个删除数据库然后恢复数据库的T-SQL脚本。 The script runs against a SQL Server 2008 database. 该脚本针对SQL Server 2008数据库运行。 Sometimes there is a problem with the backup file and the database gets stuck in the restoring state. 有时备份文件存在问题,数据库卡在恢复状态。

IF EXISTS (SELECT 1 FROM master.dbo.sysdatabases WHERE name = 'dbname')
BEGIN
    ALTER DATABASE [dbname]
    SET SINGLE_USER WITH 
    ROLLBACK IMMEDIATE
END

IF EXISTS (SELECT 1 FROM master.dbo.sysdatabases WHERE name = 'dbname')
BEGIN
    DROP DATABASE [dbname]
END

RESTORE DATABASE [dbname]
FROM  DISK = N'C:\dbname.bak'
WITH  FILE = 1,
NOUNLOAD,
STATS = 10

The next time the script runs the script generates the error message 脚本下次运行时脚本会生成错误消息

ALTER DATABASE is not permitted while a database is in the Restoring state.

What is the best way to check if the database is in the restoring state before trying to run the ALTER DATABASE command? 在尝试运行ALTER DATABASE命令之前,检查数据库是否处于恢复状态的最佳方法是什么?

EDIT: The RESTORE DATABASE command that I'm running doesn't use the NO RECOVERY option. 编辑:我正在运行的RESTORE DATABASE命令不使用NO RECOVERY选项。

It sounds as though you are performing a database restore with the NORECOVERY option. 听起来好像是使用NORECOVERY选项执行数据库恢复。 The reason you would want to do this is if you were planning to apply subsequent transaction log backups after the initial restore. 您希望这样做的原因是您计划在初始恢复后应用后续事务日志备份。

If you only wish to restore a single database backup then remove the NORECOVERY clause. 如果您只希望还原单个数据库备份,请删除NORECOVERY子句。 If you are restoring transaction log backups, the final restore must be done without the NORECOVERY clause or if the last was applied with NORECOVERY you can RESTORE DATABASE DbName WITH RECOVERY to finalize. 如果要还原事务日志备份,则必须在没有NORECOVERY子句的情况下完成最终还原,或者如果使用NORECOVERY应用最后一个,则可以RESTORE DATABASE DbName WITH RECOVERY来完成。

To answer your question: 回答你的问题:

Method 1 方法1

SELECT DATABASEPROPERTYEX ('DatabaseName', 'Status')

See SQL Server Books Online: DATABASEPROPERTYEX (Transact-SQL) 请参见SQL Server联机丛书: DATABASEPROPERTYEX(Transact-SQL)

Method 2 方法2

Review the sys.databases system view in order to determine the current state of a database. 查看sys.databases系统视图以确定数据库的当前状态。 For example: 例如:

SELECT
    state,
    state_desc
    FROM sys.databases
WHERE [name] = 'DatabaseName'

A state of 1 = RESTORING 状态为1 =恢复

See Sys.Databases for documentation regarding this system view. 有关此系统视图的文档,请参阅Sys.Databases

SELECT DATABASEPROPERTYEX ('MyDb','Status')

Others had a similar problem doing a RESTORE with pyodbc . 其他人在使用pyodbc进行RESTORE时遇到了类似的问题 My variation of the problem (with similar symptoms to yours) turned out to be an incorrect bak file. 的问题的变化 (与你的症状相似)原来是一个不正确的bak文件。 This can be discovered with the following T-SQL, looking for incorrect .mdf or .ldf file names or database names: 可以使用以下T-SQL发现这一点,查找不正确的.mdf.ldf文件名或数据库名称:

RESTORE FILELISTONLY FROM DISK = N'C:\dbname.bak'

Method 2: 方法2:

SELECT
    state,
    state_desc
     FROM sys.databases
WHERE [name] = 'Databasename'

It is give me exact result. 它给了我确切的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM