Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 programatically restore SQL database

Author  Topic 

ajcool123
Starting Member

8 Posts

Posted - 2008-05-15 : 10:45:21
Hi Guys,

I am trying to figure out a way to restore a database programatically. Here is how I do it now. Right click on the database, select Restore. Browse out to my computer and find the .BAK file. Select the database and go into 'Restore Options' I have to physically change the .MDF and .LOG paths since I am getting this backup is not coming from my computer. I select 'Overwrite the existing database' then click restore.

My problem is that I have to change the paths to the MDF and LOG files. How could I automate this? Thank you!!

jen
Master Smack Fu Yak Hacker

4110 Posts

Posted - 2008-05-15 : 10:48:28
you need to specify a default path in your program for the machine you're restoring into... standardization is the way to go

or are you using ssms? if yes, then there is no way, because those textboxes get filled up automatically based on the backup information


quote:
Originally posted by ajcool123

Hi Guys,

I am trying to figure out a way to restore a database programatically. Here is how I do it now. Right click on the database, select Restore. Browse out to my computer and find the .BAK file. Select the database and go into 'Restore Options' I have to physically change the .MDF and .LOG paths since I am getting this backup is not coming from my computer. I select 'Overwrite the existing database' then click restore.

My problem is that I have to change the paths to the MDF and LOG files. How could I automate this? Thank you!!



--------------------
keeping it simple...
Go to Top of Page

ajcool123
Starting Member

8 Posts

Posted - 2008-05-15 : 11:15:39
SSMS? I am using sql server management studio, yeah the boxes are being filled by the paths from the .bak of whoever backed up the initial database.

There is NO way I can acomplish this with a script or even writing some code to do it? I would love to have a little prog that will all the input of .bak location, .mdf location, .log location, and give database name, and it will restore it.

what does anyone think?
Go to Top of Page

Van
Constraint Violating Yak Guru

462 Posts

Posted - 2008-05-15 : 11:25:16
What about the RESTORE DATABASE command. You can use the WITH MOVE option to move the files where you need them. It's well documented in BOL.
Go to Top of Page

rohitkumar
Constraint Violating Yak Guru

472 Posts

Posted - 2008-05-15 : 11:27:57
see syntax at the end on this link

http://searchsqlserver.techtarget.com/tip/0,289483,sid87_gci1173442,00.html

" SSMS? I am using sql server management studio,"
Go to Top of Page

ajcool123
Starting Member

8 Posts

Posted - 2008-05-15 : 11:28:26
okay cool how would something like that look? what would i be moving is my question, i just need to change the paths to the MDF and LOG file i would think right? i appreciate all the help.
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2008-05-15 : 12:58:30
Here is an example restore command using the MOVE option to move the files to a different path:

RESTORE DATABASE dbName
FROM DISK = 'F:\Backup\dbName.bak'
WITH REPLACE, MOVE 'dbName_Data' TO 'F:\Data\dbName.mdf', MOVE 'dbName_Log' TO F:\Log\dbName.ldf'

You will need to determine the logical names of your database. You can do that with RESTORE FILELISTONLY.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Database maintenance routines:
http://weblogs.sqlteam.com/tarad/archive/2004/07/02/1705.aspx
Go to Top of Page

ajcool123
Starting Member

8 Posts

Posted - 2008-05-15 : 13:41:16
Thanks for all the help. I found a very easy way to do this. On the restore screen you can just click "Script" at the top and output the code to a script. This makes it so much easier to get a perfect script with all desired options.

Go to Top of Page
   

- Advertisement -