[フレーム]

How to Recover a SQL Server Database in Emergency Mode (Step-by-Step)

When a SQL Server database becomes suspect or corrupted, SQL Server might automatically set it into EMERGENCY mode. This mode allows limited access for troubleshooting and repair. In this article, we’ll explore how to recover a database from EMERGENCY mode safely — using step-by-step SQL commands and Microsoft-approved recovery practices.

🚨 What Is Emergency Mode?

Emergency mode is a special state in SQL Server that allows administrators to access a database marked as corrupt or inaccessible. It’s read-only, single-user, and bypasses the transaction log for emergency repair or data extraction.

Typical reasons your database enters this state include:

  • Missing or corrupt transaction logs

  • Hardware failure or disk corruption

  • Unexpected shutdowns or SQL Server crashes

  • File system or drive letter changes

🧠 Step-by-Step Guide to Recover a Database from Emergency Mode

🧩 Step 1. Check Database State

Use this command to verify the current state of your database:

SELECT name, state_desc 
FROM sys.databases 
WHERE name = 'YourDatabaseName';

If the state shows as EMERGENCY or SUSPECT, proceed to recovery.

🧩 Step 2. Set Database to Emergency Mode (Manually)

If SQL Server has not already done this, you can manually put your database into EMERGENCY mode:

ALTER DATABASE YourDatabaseName SET EMERGENCY;
GO

This gives you sysadmin-level access to inspect and fix the database.

🧩 Step 3. Run Consistency Check (DBCC CHECKDB)

Now, check the physical and logical integrity of your database:

DBCC CHECKDB (YourDatabaseName);
GO

If you see errors like "Msg 824, 825, or 826", it means corruption exists.

🧩 Step 4. Set Database to Single User Mode

Before performing repairs, you must ensure no other connections interfere:

ALTER DATABASE YourDatabaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE;
GO

🧩 Step 5. Attempt to Repair the Database

There are multiple repair levels; the most commonly used for severe corruption is:

DBCC CHECKDB (YourDatabaseName, REPAIR_ALLOW_DATA_LOSS);
GO

⚠️ Warning: As the name suggests, this may cause some data loss.
Always attempt to restore from backup first if available.

If you have a valid recent backup, do not run this — instead, restore using RESTORE DATABASE.

🧩 Step 6. Set Database Back to Multi-User Mode

Once the repair completes successfully, return the database to normal operation:

ALTER DATABASE YourDatabaseName SET MULTI_USER;
GO

Then, verify the database state again:

SELECT name, state_desc FROM sys.databases WHERE name = 'YourDatabaseName';
GO

It should now show as ONLINE.

🧰 Optional: Restore from Backup Instead of Repair

If you maintain regular backups, restoring is the safest route:

RESTORE DATABASE YourDatabaseName 
FROM DISK = 'D:\Backups\YourDatabaseName.bak' 
WITH REPLACE;
GO

This avoids potential data loss caused by emergency repairs.

🪄 Example Scenario

Let’s assume your database StudentDB is marked as SUSPECT.
Here’s a real-world recovery script:

USE master;
GO
ALTER DATABASE StudentDB SET EMERGENCY;
GO
ALTER DATABASE StudentDB SET SINGLE_USER;
GO
DBCC CHECKDB (StudentDB, REPAIR_ALLOW_DATA_LOSS);
GO
ALTER DATABASE StudentDB SET MULTI_USER;
GO

After running this, StudentDB should return to a normal operational state.

🧩 Verify and Backup Immediately

Once your database is online:

  1. Validate key tables and data integrity.

  2. Take a full backup immediately:

    BACKUP DATABASE StudentDB TO DISK = 'D:\Backup\StudentDB_Recovered.bak';
    GO
    
  3. Monitor your SQL Server logs for any recurring I/O or consistency errors.

🧭 Best Practices to Prevent Emergency Mode Issues

  • ✅ Always maintain daily full backups and transaction log backups.

  • ✅ Enable SQL Server alerts for error codes 823, 824, 825.

  • ✅ Store backups on different physical drives or cloud storage (e.g., Azure Blob, AWS S3).

  • ✅ Use ECC RAM and RAID storage for database servers.

  • ✅ Schedule regular DBCC CHECKDB checks in SQL Agent jobs.

🌐 External References & Further Reading

Here are some official and community resources for deeper understanding:

  • 🔗 Microsoft Docs: DBCC CHECKDB (Transact-SQL)

  • 🔗 Microsoft Learn: ALTER DATABASE (Transact-SQL)

  • 🔗 SQLShack: Recover SQL Server database from emergency mode

  • 🔗 Redgate: Handling a Suspect Database

  • 🔗 Brent Ozar Blog: DBCC CHECKDB – The Complete Guide

🧾 Summary

StepCommandPurpose
1ALTER DATABASE SET EMERGENCYGain access to damaged DB
2DBCC CHECKDBDiagnose corruption
3REPAIR_ALLOW_DATA_LOSSAttempt recovery
4SET MULTI_USERRestore normal mode
5BACKUP DATABASESecure recovered data

⚙️ Final Thoughts

Recovering from SQL Server Emergency Mode is a last-resort operation. Always prefer restoring from backup, and only use REPAIR_ALLOW_DATA_LOSS if you have no valid backup. Once recovered, perform integrity checks and establish a robust backup policy to prevent future crises.

People also reading
Membership not found

AltStyle によって変換されたページ (->オリジナル) /