last day (14 days later) » 

3:05 PM
-3
A: SQL Server specify mdf file name when restoring database

pmbAustinHere's what I use (in a batch file) to restore a specific already-existing database from backup. The "Replace" causes it to over-write the existing files, so I don't need to specify them. RESTORE DATABASE [%DatabaseName%] FROM DISK = N'%DestinationFolder%\%backupFile%' WITH FILE = 1, NOUNLOA...

 
The OP wants to restore a copy of the database, with a different name, not replace the existing version.
 
Understood, which is why I put in the line: "This may not serve your needs exactly, but I thought it might be useful for others searching on this issue." ... of course, creating a database with the new name, this script allows you to restore any backup to that new DB, which achieves a similar effect.
 
this script allows you to restore any backup to that new DB - but not if the data/log files already exist, which is the OP's whole problem.
 
Actually it does... which is the whole point. I use this to restore a database from a backup of a different database. The "REPLACE" causes it to overwrite the mdf/ldf files of the existing database that is being restored to. It works great for me. SQL 2008 and up.
 
sigh but overwrite the mdf/ldf files of the existing database is not what the OP is trying to do.
 
3:05 PM
I don't think you understand. By "existing" database, I meant the database that is being restored. Which I would think is exactly what you want to do. To clarify: I have a database B on my local system. I make a backup of database A on a different server, copy the *.bak file to my local system, and then use this script to restore the *.bak file to database B. It correctly restores. Obviously database A isn't touched. And my Database B is now a copy of database A.
 
I don't think you quite understand the OP's actual problem. They want to backup database A on their own machine, restore it as database B. They don't want to use WITH REPLACE for that - database B doesn't exist yet, and they want database A and database B to co-exist. They can't just restore database B WITH REPLACE without using WITH MOVE because the mdf/ldf files already exist (and WITH REPLACE would only be useful if database B already existed). Your answer doesn't address that problem, nor do your comments. You are trying to solve a completely different problem.
Why don't you try this - create a database, say DB_A, then back it up, then use your script to try to restore that to the same server as DB_B WITH REPLACE, and see how far you get.
 
Yes, I understand all that. My POINT (other than this answer is relevant to people who might find this in a search) is that you can easily CREATE a database, and then restore to it. Boom. Done. And database A and B can co-exist. Back up A, create B, restore a.bak to B. It works. I do it every day. Again, I specified in my comment it wasn't specific to his problem, but the reality is, it can be used to solve his problem as well. I don't see your issue with my comments or answer, to be honest.
 
If it's not specific to this problem, then it shouldn't be an answer. I could post an answer that shows how to restore a database from a striped backup and change the growth settings of the data files. As long as I put a disclaimer that it isn't relevant to this question, does that make it ok?
 
What part of "create database B" before restoring to it WITH REPLACE are you not understanding? Just curious.
 
@pmbAustin how would this be useful for others who are searching this? I imagine if they are searching this then they have the same problem as the OP. A problem that this answer doesn't address since you are talking about restoring over and not creating a new copy on the same instance.
 
3:05 PM
@pmbAustin Did you actually try that? I get: Msg 1834, Level 16, State 1, Line 7 The file 'C:\Program Files\Microsoft SQL Server\MSSQL12.SQL14\MSSQL\DATA\splunge.mdf' cannot be overwritten. It is being used by database 'splunge'. Msg 3156, Level 16, State 4, Line 7 File 'splunge' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL12.SQL14\MSSQL\DATA\splunge.mdf'. Use WITH MOVE to identify a valid location for the file.
Msg 1834, Level 16, State 1, Line 7 The file 'C:\Program Files\Microsoft SQL Server\MSSQL12.SQL14\MSSQL\DATA\splunge_log.ldf' cannot be overwritten. It is being used by database 'splunge'. Msg 3156, Level 16, State 4, Line 7 File 'splunge_log' cannot be restored to 'C:\Program Files\Microsoft SQL Server\MSSQL12.SQL14\MSSQL\DATA\splunge_log.ldf'. Use WITH MOVE to identify a valid location for the file. Msg 3119, Level 16, State 1, Line 7 Problems were identified while planning for the RESTORE statement. Msg 3013, Level 16, State 1, Line 7 RESTORE DATABASE is terminating abnormally.
So, we're back to having to use WITH MOVE anyway, regardless of whether you create the database first. It still tries to replace the data/log files from the original database (and it has to, because they may have different sizes).
 
@AaronBertrand something tells me he's not going to see the reasoning in that.
 
(shrug) I use this script every single day to restore the backup from a different database to my local database copy. And it works fine for me. Every day for years. But fine, I'll update my answer with the far more elaborate script I use to create-or-restore any database.
 
I'm sure his / her intentions are well, flag for deletion is nicer than all the chit chat guys.
 
I've updated my answer with a more relevant script. Please review THAT before commenting further. Sheesh, you try to help...
 
@JonH Unfortunately moderators will still tend to view this as an attempt to answer the question (even if it actually attempts to answer a completely different question).
 
3:05 PM
@AaronBertrand... try this: Manually restore the backup file to the newly created database once (using SSMS GUI or whatever). From that point on, my first script will work fine. I have no idea why SQL Server needs to be 'primed' in this way, but once done manually once (with the correct mdf/ldf file paths specified), the restore to that same DB will continue to work without the WITH MOVE. Also please reference my update to the answer to deal with the more general case programmatically... restore any DB (existing or not) WITH MOVE.
 
Do you realize that what you've done by "priming" the backup file to the new database using the SSMS GUI or whatever is actually restoring the database WITH MOVE?
 
...and you realize that once done, you don't need to do it again, and my original script works precisely like I said it does (and it works for me daily)? And you realize I updated my answer with a second script that does the WITH MOVE? (I feel like we're talking past each other)
 
Multiple steps for no reason other than to make your original, irrelevant answer slightly more relevant. It's not needed to do all that work. All the OP needed was to add WITH MOVE options to their existing RESTORE command.
 
... and my second script will automate that, so they don't have to look the values and hand-edit a script with those values. They can just name the DB and the *.bak file, and go to town. If you think that's neither useful nor relevant, then we'll just have to agree to disagree. Completely.
 

last day (14 days later) »