question

garethl avatar image
garethl asked

SQL db Restore question

Hello all I am about to restore a new backup of a db on SQL-Server 2012 My question is as follows : How do I restore a new copy over a old copy **but** keep the existing data in the old copy? Any help would be appreciated Thank you very much for your answers
sql-serversql-server-2012
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

ThomasRushton avatar image
ThomasRushton answered
Back up the old database and restore it as something else? Or restore from the old backup into another database rather than overwrite the existing? Either way, if your intention is to merge the two together, you'll need to write some code to do that.
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Graeme_Cracker avatar image
Graeme_Cracker answered
USE [master] GO ---------------------------------------------------------------------------- --Create a backup of your database and verify it. ---------------------------------------------------------------------------- BACKUP DATABASE MyDatabase TO DISK='C:\Backups\MyDatabase.bak' WITH COPY_ONLY, COMPRESSION, STATS=10; GO RESTORE VERIFYONLY FROM DISK='C:\Backups\MyDatabase.bak' WITH STATS=10; GO ---------------------------------------------------------------------------- --Rename your database as OLD (to keep the data for review) ---------------------------------------------------------------------------- EXECUTE sp_renamedb N'MyDatabase', N'MyDatabase_OLD'; GO ---------------------------------------------------------------------------- --Rename the files as well, so you don't accidentally delete the wrong one. --This is a logical change. You still have to actually rename the files. ---------------------------------------------------------------------------- ALTER DATABASE MyDatabase_OLD MODIFY FILE (NAME = MyDatabase_Dat, FILENAME = N'C:\Data\MSSQL\Data\MyDatabase_OLD.mdf'); GO ALTER DATABASE MyDatabase_OLD MODIFY FILE (NAME = MyDatabase_log, FILENAME = N'C:\Data\MSSQL\Data\MyDatabase_OLD_log.ldf'); GO ---------------------------------------------------------------------------- --Take the "OLD" database offline so you can rename the files. ---------------------------------------------------------------------------- ALTER DATABASE [MyDatabase_OLD] SET OFFLINE GO ---------------------------------------------------------------------------- --> Here is where you move and/or rename the files: mdf and ldf to new names. --> You can use Windows File Explorer or your favorite file management tool. ---------------------------------------------------------------------------- ---------------------------------------------------------------------------- --Bring the OLD database ONLINE so you can access it. ---------------------------------------------------------------------------- ALTER DATABASE [MyDatabase_OLD] SET ONLINE GO ---------------------------------------------------------------------------- --RESTORE MyDatabase from the backup you made. Files will go into the original names. ---------------------------------------------------------------------------- RESTORE DATABASE MyDatabase FROM DISK='C:\Backups\MyDatabase.bak' WITH STATS=10; GO
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

DazyParker avatar image
DazyParker answered
There are two options to backup the database 1. Append to the existing backup set 2. Overwrite all existing backup set
10 |1200

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.