Link to home
Start Free TrialLog in
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)Flag for United States of America

asked on

Script to Restore DB

I am distributing a DB to laptops running SQL Server Express. The laptop, using VBA from an Office application, looks for a backup of the server-based DB in a predetermined location and, if it is more recent than the last one restored, restores it.

I need T-SQL script to do the restore given the path to the backup. Do I have to delete the existing local DB first? The database name does not have to be changed - it should be the same as on the server-based DB.

Kevin
ASKER CERTIFIED SOLUTION
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zorvek (Kevin Jones)

ASKER

I suspected that that is what is going on, but how is in the information pulled in step 1 used in step 2 when the only values are enclosed in single quotes? Where is the reference to the objects/data retrieved in step 1?

Also, does it matter where the SQL Server files are located? Can I put them anywhere I want?

Kevin
What if I don't want to worry about the location of the mdf and ldf files and want them placed in the default location when I create a new DB, or overwrite the existing ones if the DB is already in place? And will the above script work in both cases?

Kevin
In case you want them in default location, it is fine not to check for the logical filenames. But if you want to place them in different drives, or you want to change the name of the physical mdf and ldf files, the information from fileheader information is really helpful.
So what would the script look like to do the restore to the default local directory? Something as simple as:

RESTORE DATABASE DatabaseName FROM DISK = 'C:\...\DatabaseName.bak'

?

Kevin
Yes. Without the move part.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Guys, I'm a novice here and not quite following.

Here is the scenario. A backup of a server database is created. I want to distribute it to multiple workstations so they can have their own copy when offline. This process will happen daily. At first the local DB will not be present. On subsequent days it will be present. I have no desire to locate the DB in any specific location - the default location is fine. What will the script look like to restore the DB to the local machine when it might or might not already be present? All I know is the file path to the backup file and how to connect to the local SQL Server.

Will this work?

RESTORE DATABASE DatabaseName FROM DISK = 'C:\...\DatabaseName.bak'

Kevin
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK, now I get it! Thank you!

One more:

How do I determine what the values for 'YourMDFLogicalName' and 'YourMDLLogicalName' are?

Kevin
Kinda hoping for more responses a little more quickly. I'm asking a new question to continue.