Learn more about SQL Server tools

mssqltips logo
 

Tutorials          DBA          Dev          BI          Career          Categories          Webcasts          Scripts          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Webcasts

DBA    Dev    BI    Categories

 

SQL Server Database Stuck in Restoring State


By:   |   Last Updated: 2018-09-06   |   Comments (2)   |   Related Tips: More > Restore

Problem

My SQL Server database is in a restoring state. How does this happen and how can I access my SQL Server database?

object explorer
Solution

In this article we will show reasons why a SQL Server database is in a restoring state and how you can get access to a database in a restoring state. It is not a very common problem, but when it happens it can be a big headache.  In this article, we will see different reasons and possible solutions to solve this.

These steps will work for any version of SQL Server.

SQL Server database in RESTORING state after a restore

Usually, the restoring state happens when you are restoring a database. Here we will walk through an example of this. 

I will create a full backup and log backup.

BACKUP DATABASE [earnings] TO DISK = N'c:\sql\earnings.bak' 
WITH NOFORMAT, NOINIT, NAME = N'earnings-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO

BACKUP LOG [earnings] TO DISK = N'C:\sql\earnings_LogBackup_2018-06-02_12-42-07.bak' 
WITH NOFORMAT, NOINIT, NAME = N'earnings_LogBackup_2018-06-02_12-42-07', SKIP, NOREWIND, NOUNLOAD, STATS = 10

Once we have the backups, we will restore the backups.

In order to restore the full and log backup we need to use the NORECOVERY option for the full restore. So, if we just restore the full backup as follows:

RESTORE DATABASE [earnings] 
FROM DISK = N'c:\sql\earnings.bak' WITH NORECOVERY, NOUNLOAD, STATS = 10

The database will now be in a restoring state.  If we forget to restore additional backups, the database will be stuck in this mode.

object explorer

To finalize the restore and access the database we need to restore the log backup as follows:

RESTORE LOG [earnings]
FROM DISK = N'c:\sql\earnings_LogBackup_2018-06-02_12-42-07.bak'

SQL Server database in RESTORING state after doing backup log with NORECOVERY

Another reason your database can be in restoring state is when you backup the tail of the log using the NORECOVERY option as shown below.

BACKUP DATABASE [earnings] TO DISK = N'c:\sql\earnings.bak' 
WITH NOFORMAT, NOINIT,  NAME = N'earnings-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO

BACKUP LOG [earnings] TO DISK = N'C:\sql\earnings_LogBackup_2018-06-02_12-42-07.bak' 
WITH NOFORMAT, NOINIT, NAME = N'earnings_LogBackup_2018-06-02_12-42-07', SKIP, NOREWIND, NOUNLOAD, NORECOVERY, STATS = 10

This will cause the database to change to a restoring state. 

To fix this you can restore the database backups as shown above.

Make a SQL Server database in RESTORING state accessible without restoring backups

If the database is stuck in the restoring state and you don't have additional backups to restore, you can recover the database using the following command:

RESTORE DATABASE [earnings] WITH RECOVERY

Once you issue this command, the database will be useable, but you won't be able to restore any additional backups for this database without starting all over again with the full backup.

For more details about restoring a database in a restoring state, refer to this article Recovering a database that is in the restoring state.

SQL Server database in RESTORING state for Database Mirroring

Another reason your database is in a restoring state is that it is part of SQL Server Database Mirroring. Database Mirroring is a solution that allows you to have high availability for your database. If there is a database failure on the primary database, the secondary replica database on a different server will take over the database operations. The main database is the Principal Server, the secondary is the Mirror Server and optionally you can have another Mirror Server.

Here is an example.  We can see on the left that the Principal server is where the database is accessible.  On the right we can see the Mirror that is in a Restoring state.

database mirroringMirror16

For more information about Database Mirroring in SQL Server, refer to this link: Configure SQL Server Database Mirroring Using SSMS.

In Database Mirroring, the Mirror Server is in Restoring state until a Failover is done. To access a SQL Server database that is in a restoring state when it is part of Database irroring, you can do a manual or automatic failover from the Principal to the Mirror.

To do an automatic failover, refer to the following link: Role Switching During a Database Mirroring Session (SQL Server).

To break the mirror, you will need to select the database and go to the mirroring page and select the remove mirroring button. The following article shows how to do it.  Once removed, the mirroring database will return to the normal state and you can backup and restore the database as a normal database.

SQL Server database in RESTORING state for Log Shipping

SQL Server Log Shipping allows to you to backup the transaction logs and send and restore the backups on a different server in order to have replicas of the database in case the primary servers fails.

Log Shipping puts the database in a Standby state with recovery or with no recovery. The no recovery mode will show the Log Shipping database in a Restoring state as shown below.

log shipping status in SSMS in restoring state

Here is a link to change the state to avoid the restoring state: Change the restore mode of a secondary SQL Server database in Log Shipping with SSMS.

SQL Server database stuck in RESTORING state after restarting the machine

Sometimes the database is in a restoring state after restarting the machine or for some other reason. It usually happens with big databases when a long transaction is in progress and an unexpected server shutdown or restart occurs.

object explorer

If you have this problem, try this first:

RESTORE DATABASE [databasename] WITH RECOVERY

If you receive an error that the database is in use, try to set the user to single user mode:

USE master;
GO

ALTER DATABASE Database_name
SET SINGLE_USER
WITH ROLLBACK IMMEDIATE;

Then try the restore with recovery command again. 

Once restored, you can set to multiple user mode using the following T-SQL command:

USE master;
GO

ALTER DATABASE Database_name
SET MULTI_USER;
GO

Also, make sure you are on the latest service pack or cumulative update. There are several issues and patches related to recovery problems.

Here are some links with the service packs and cumulate updates:

Also, you will should review the Error Log and the Windows Event Viewer to check for errors.  Refer to these links:

Conclusion

In this article, we saw different reasons why a database could be in a Restoring state. Hopefully this will be helpful the next time you are troubleshooting this issue.

Next Steps

If you have more questions, feel free to ask in the comments section below.

For more information refer to these links:



Last Updated: 2018-09-06


get scripts

next tip button



About the author
MSSQLTips author Daniel Calbimonte Daniel Calbimonte is a Microsoft SQL Server MVP, Microsoft Certified Trainer and Microsoft Certified IT Professional.

View all my tips
Related Resources




Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name    *Email    Email me updates 


Signup for our newsletter
 I agree by submitting my data to receive communications, account updates and/or special offers about SQL Server from MSSQLTips and/or its Sponsors. I have read the privacy statement and understand I may unsubscribe at any time.



    



Thursday, May 30, 2019 - 1:05:18 AM - GVV Vinay Back To Top

Thank you so much, it helped.


Tuesday, February 26, 2019 - 10:25:26 PM - Sureindran Nadesan Back To Top

If nothing works, restore with a empty db.


Learn more about SQL Server tools