97

I am trying to create a new database from an old backup of database on the same server. When using SQL server management studio and trying to restore to the new DB from the backup I get this error

System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than the existing 'test' database. (Microsoft.SqlServer.Smo)

after googling around I found this piece of code

    RESTORE DATABASE myDB

 FROM DISK = 'C:\myDB.bak'

 WITH MOVE 'myDB_Data' TO 'C:\DATA\myDB.mdf',

MOVE 'myDB_Log' TO 'C:\DATA\myDB_log.mdf'
GO

I was wondering will the move statements mess with the database that the backup came from on that server?

2
  • Suggest you try: dba.stackexchange.com Commented Apr 24, 2012 at 14:41
  • no, restores don't affect their original source db (unless that's where you're restoring it to) Commented Jan 16, 2020 at 11:28

6 Answers 6

153

What I should to do:

  • Click on 'Restore Database ...' float menu that appears right clicking the "Databases" node on SQL Server Management Studio.
  • Fill wizard with the database to restore and the new name.
  • Important If database still exists change the "Restore As" file names in the "Files" tab to avoid "files already in use, cannot overwrite" error message.

What I do

IDk why I prefer to do this:

  • I create a blank target database with my favorite params.
  • Then, in "SQL Server Management Studio" restore wizard, I look for the option to overwrite target database. It is in the 'Options' tab and is called 'Overwrite the existing database (WITH REPLACE)'. Check it.
  • Remember to select target files in 'Files' page.

You can change 'tabs' at left side of the wizard (General, Files, Options)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, I had tried using the with Replace option in the Management Studio before But didnt do the Files part. Did what you suggested and the databse is being created now.
I ran into this issue when trying solution above --> "The backup set holds a backup of a database other than the existing" what worked brilliantly for me was solution found here: stackoverflow.com/questions/10204480/…
104

It's even possible to restore without creating a blank database at all.

In Sql Server Management Studio, right click on Databases and select Restore Database... enter image description here

In the Restore Database dialog, select the Source Database or Device as normal. Once the source database is selected, SSMS will populate the destination database name based on the original name of the database.

It's then possible to change the name of the database and enter a new destination database name.

enter image description here

With this approach, you don't even need to go to the Options tab and click the "Overwrite the existing database" option.

Also, the database files will be named consistently with your new database name and you still have the option to change file names if you want.

4 Comments

This solution worked for me but only if I specifically change the "Restore As" file names in the "Files" tab, to something that did not exist already. Otherwise I get a "files already in use, cannot overwrite" failed restore error message.
This worked for me as well! :) I had to disable Take tail-log backup before restore since I was getting an error as described here stackoverflow.com/a/29497004/5588197
every time I do this it puts the source database (say we restore X to new_X, then the source database is X), into an unusable "Restoring..." state where it is not queryable
worked in mssql 2019
11

Checking the Option "Overwrite the existing database" worked for me:

enter image description here

1 Comment

This answer needs more information; what is the name of the window that shows these menu options and how does one access it?
1

Think of it like an archive. MyDB.Bak contains MyDB.mdf and MyDB.ldf.

Restore with Move to say HerDB basically grabs MyDB.mdf (and ldf) from the back up, and copies them as HerDB.mdf and ldf.

So if you already had a MyDb on the server instance you are restoring to it wouldn't be touched.

Comments

0

The script in the question is just missing the replace statement so the restore script will be

RESTORE DATABASE myDB

 FROM DISK = 'C:\myDB.bak' ,

 WITH MOVE 'myDB_Data' TO 'C:\DATA\myDB.mdf',
,
MOVE 'myDB_Log' TO 'C:\DATA\myDB_log.mdf' ,  NOUNLOAD,  REPLACE,  STATS = 5

1 Comment

Your answer adds more than just the replace keyword.
0

Use this procedure

create or alter procedure spDatabaseCreateFromBak(
    @NewDatabaseName nvarchar(128),
    @BackupFilePath nvarchar(500),
    @DryCall bit = null,
    @Debug int = null
) as begin
    declare @DefaultDataPath nvarchar(max) = cast(serverproperty('InstanceDefaultDataPath') as nvarchar(max)); 
    declare @DefaultLogPath nvarchar(max) = cast(serverproperty('InstanceDefaultLogPath') as nvarchar(max))

    -- produce restore sql
    declare @dsql nvarchar(max)
    begin
        create table #FileList (
            LogicalName nvarchar(128),
            PhysicalName nvarchar(260),
            Type char(1),
            FileGroupName nvarchar(128),
            Size bigint,
            MaxSize bigint,
            FileId bigint,
            CreateLSN numeric(25,0),
            DropLSN numeric(25,0),
            UniqueId uniqueidentifier,
            ReadOnlyLSN numeric(25,0),
            ReadWriteLSN numeric(25,0),
            BackupSizeInBytes bigint,
            SourceBlockSize int,
            FileGroupId int,
            LogGroupGUID uniqueidentifier,
            DifferentialBaseLSN numeric(25,0),
            DifferentialBaseGUID uniqueidentifier,
            IsReadOnly bit,
            IsPresent bit,
            TDEThumbprint varbinary(32),
            SnapshotUrl nvarchar(360)
        )

        insert into #FileList
        exec ('restore filelistonly from disk = ''' + @BackupFilePath + '''')

        if @Debug >= 2
            select [FileList] = '#FileList', * from #FileList f;

        set @dsql = (
            select string_agg(line, char(10))
            from (
                select line = concat('restore database [',@NewDatabaseName,']')
                union all
                select line = concat('from disk = ''',@BackupFilePath,''' with')
                union all
                select concat('move N''', f.LogicalName, ''' to N''', NewPath, ''',')
                from #FileList f
                outer apply (
                    select NewPath = concat(@DefaultDataPath, s.FileName)
                    from (
                        select top 1 FileName = s.value
                        from string_split(f.PhysicalName, '\', 1) s
                        order by s.ordinal desc
                    ) s
                ) np
                union all
                select line = 'replace, recovery'
            ) t
        );
    end

    if @Debug > 0
        print @dsql;

    if isnull(@DryCall, 0) = 0
        exec(@dsql);
end

Example:

exec spDatabaseCreateFromBak 
    @BackupFilePath = N'D:\Work\Exports\WideWorldImporters-Full.bak'
    , @NewDatabaseName = N'WideWorldImporters'
    , @DryCall = 0
    , @debug = 2

Which would produce such sql:

restore database [WideWorldImporters]
from disk = 'D:\Work\Exports\WideWorldImporters-Full.bak' with
move N'WWI_Primary' to N'D:\SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA\WideWorldImporters.mdf',
move N'WWI_UserData' to N'D:\SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA\WideWorldImporters_UserData.ndf',
move N'WWI_Log' to N'D:\SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA\WideWorldImporters.ldf',
move N'WWI_InMemory_Data_1' to N'D:\SQL Server\MSSQL16.MSSQLSERVER\MSSQL\DATA\WideWorldImporters_InMemory_Data_1',
replace, recovery

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.