Monday, October 17, 2011

Sql-Server: Determining the status of a database

I needed to determine if a database was being restored or not. The easy way is to determine the status of the database using the following script:

select [name], databasepropertyex([name], 'Status') as Status
from sysdatabases
where [name] = 'DatabaseName'

If the database is being restored, then the status will state: RESTORING

Other valid values for Status are:

ONLINE = Database is available for query.
OFFLINE = Database was explicitly taken offline.
RESTORING = Database is being restored.
RECOVERING = Database is recovering and not yet ready for queries.
SUSPECT = Database did not recover.
EMERGENCY = Database is in an emergency, read-only state. Access is restricted to sysadmin members

No comments: