Restore SQL Server database and overwrite existing database


By:

Overview

The RESTORE ... WITH REPLACE option allows you to overwrite an existing database when doing a restore.  In some cases when you try to do a restore you may get an error that says "The tail of the log for the database .. has not been backed up".

Explanation

The RESTORE ... WITH REPLACE allows you to write over an existing database when doing a restore without first backing up the tail of the transaction log.  The WITH REPLACE basically tells SQL Server to just throw out any active contents in the transaction log and move forward with the restore.

If you try to restore using T-SQL commands you will get this error message:

Msg 3159, Level 16, State 1, Line 1
The tail of the log for the database "AdventureWorks" 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 1
RESTORE DATABASE is terminating abnormally.

If you try to restore using SQL Server Management Studio you will see this error message:

e4


T-SQL

Restore full backup using WITH REPLACE
The command below will restore the database and disregard any active data in the current transaction log.

RESTORE DATABASE AdventureWorks FROM DISK = 'C:\AdventureWorks.BAK'
WITH REPLACE
GO

SQL Server Management Studio

To restore using SSMS do the following, on the options page for the restore select "Overwrite the existing database".

e5






Comments For This Article




Tuesday, August 15, 2023 - 2:00:06 AM - samsam Back To Top (91488)
it works!!! thank you so much!

Friday, March 3, 2023 - 6:59:54 PM - Rick Back To Top (90981)
This article saved the day.
The "Overwrite existing database option" was unchecked. Checking it worked like a charm.

Thanks

Monday, November 29, 2021 - 3:13:00 AM - Peter Mankge Back To Top (89499)
Worked very well for me. Thank you.

Monday, January 27, 2020 - 9:05:41 AM - DBA Back To Top (83996)

note: you should always tale a backup of your current database before doing a restore.


Wednesday, August 8, 2018 - 4:27:08 AM - Srinivas Back To Top (77071)

This tip really worked for me.thank you


Wednesday, July 18, 2018 - 2:12:26 AM - Edvard Korsbęk Back To Top (76667)

 Thanks!

Allways done so, but never understood why before reading this.

 

Best regards from Denmark

 

Edvard Korsbęk

 


Tuesday, November 1, 2016 - 4:05:12 PM - Mary Young Back To Top (43672)

My database has encrypted columns. 

I need some help in figuring out how to backup one database (FRDTest) and restore it into an existing database with a different name (FRDUserTest).  We’ve been able to successfully create a NEW database by restoring the backup, but cannot restore into an existing database .  

I get error 3154: The backup set holds a backup of a database other than the existing 'FRDUserTest'.  And, yes, I DO select "WITH REPLACE".

 

 















get free sql tips
agree to terms