Restoring a backup of one database to another (that already exists)?

Paul Kraemer 266 Reputation points
2022-07-14T14:58:19.533+00:00

Hi,

I am using Microsoft SQL Server Standard (64-bit) version 11.0.7507.2. I have a production database named YICSQLNJ.

Some time ago, I backed up this database to a file named C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\YICSQLNJ_20211224.bak. Wanting to create a test database that I could work with without risking actual data, I restored this backup to a new database named PK_YIC_NJ using the following command:

RESTORE DATABASE PK_YIC_NJ FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\YICSQLNJ_20211224.bak'
WITH MOVE 'YICSQLNJ' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\PK_YIC_NJ.mdf',
MOVE 'YICSQLNJ_log' TO 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\DATA\PK_YIC_NJ_log.ldf'

This worked fine at the time. Now, I have a more recent backup that I would like to restore to my test database. My question is, now that PK_YIC_NJ already exists and has data in it, will the command above still work (assuming I update the name of the .bak file to reflect the more recent backup)? Or should I first delete PK_YIC_NJ before I do this?

Any suggestions will be greatly appreciated.

Thanks in advance,
Paul

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
12,700 questions
Transact-SQL
Transact-SQL
A Microsoft extension to the ANSI SQL language that includes procedural programming, local variables, and various support functions.
4,552 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bjoern Peters 8,781 Reputation points
    2022-07-14T15:03:56.01+00:00

    There are two options:

    1.) You drop that database before and then restore it from your backup
    2.) Add "With Replace" to your RESTORE command
    RESTORE DATABASE PK_YIC_NJ FROM DISK = 'C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Backup\YICSQLNJ_20211224.bak' WITH REPLACE

    https://learn.microsoft.com/en-us/sql/t-sql/statements/restore-statements-transact-sql?view=sql-server-ver16#REPLACEoption
    https://learn.microsoft.com/en-us/sql/t-sql/statements/restore-statements-transact-sql?view=sql-server-ver16#examples

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Paul Kraemer 266 Reputation points
    2022-07-14T16:10:10.36+00:00

    Thank you Bjorn !

    0 comments No comments