55

I tried to restore a database using the following query:

ALTER DATABASE [DatabaseName] SET Single_User WITH Rollback Immediate GO
RESTORE DATABASE DatabaseName FROM DISK = 'C:\DBName-Full Database Backup'
ALTER DATABASE [DatabaseName] SET Multi_User GO

but instead of restoring the database, I am getting this error:

Msg 3159, Level 16, State 1, Line 2

The tail of the log for the database "DatabaseName" has not been backed up. Use BACKUP LOG WITH NORECOVERY to backup the log if it contains work you do not want to lose. Use the WITH REPLACE or WITH STOPAT clause of the RESTORE statement to just overwrite the contents of the log. Msg 3013, Level 16, State 1, Line 2 RESTORE DATABASE is terminating abnormally.

1
  • In my case it had happened when I already had a database with same name on the same SQL Server. I just deleted the db with the same name and ran the restore command. Note: I deleted db with the same name since it was an old copy of the db that I did not need anymore. But, if you still need the old copy you may either want to make a backup of it or rename either the old or the new db. But, if you don't have a db with the same name then do what the error message is asking to do.
    – nam
    Oct 1, 2017 at 19:26

5 Answers 5

117

The error message you are getting tells you exactly what you need to do if you don't care about the existing database or log.

RESTORE DATABASE DAtabaseName FROM DISK = 'C:\DBName-Full Database Backup' 
WITH REPLACE

In SQL Server Management Studio (Tasks > Restore), you can add the WITH REPLACE option by opening the page "Options" on the left side and ticking "Overwrite the existing database".

1
  • Had a transport issue as well. Had to Run USE master ; ALTER DATABASE RAVEN SET RECOVERY SIMPLE; Just remember to turn it back to FULL when you are done. Aug 25, 2014 at 14:36
31

For those who are using Management Studio this should work:

enter image description here

3
  • it essentially only add a [replace] keyword to [With] param list if you usually generate scripts from SSMS
    – Iman
    Oct 10, 2014 at 7:54
  • image no longer visible :/
    – rpr
    Oct 31, 2019 at 10:24
  • 1
    @rpr it's showing OK to me. BTW, I have uploaded it here for you: imgur.com/a/ki9elxa
    – Adil Malik
    Oct 31, 2019 at 14:09
7

Alternatively, you can change the database recovery model to Simple instead of Full.

  1. Right click on the database
  2. Choose properties-> option
  3. Change recovery model to simple

Then what you have written should work without producing errors.

It worked good with me.

2

Alternatively, you can change the database recovery model to "Simple" instead of "Full".

Right click on the database, choose 'properties', 'option', change "recovery model" to "simple".

Then what you have written should work without producing errors.

0

Use BACKUP LOG WITH NORECOVERY to backup the log if it contains work you do not want to lose.

Use the WITH REPLACE or WITH STOPAT clause of the RESTORE statement to just overwrite the contents of the log.

Really, that's the answer. Right there, in the message. What do you want to do? Backup the tail so is no lost? Replace the log that was not backed up? Your call.

2
  • 1
    since I work rarely with SQL Server I backup Database by going to tasks, backup and performed a backup, and now want to restore it, but it doesn't let me, so I don't exactly know the reason of above error, and most forums says it works without getting any error
    – tereško
    Sep 11, 2012 at 13:40
  • 4
    The error is warning you that if you continue with the RESTRORE, you will loose the current tail of the log. In other words, some operations were not yet backed up. It gives you two options: either backup the current tail of the log so is poreserved and you can restore it somewhere else or later or use the WITH REPLACE clause of RESTORE to instruct SQL Server that you do not care about the operations that would be lost in the current tail of the log and simply go ahead and replace the current content with the one from the backup file you are restoring. Sep 11, 2012 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.