Fully automated SQL Server Restore

Posted by hasen j on Stack Overflow See other posts from Stack Overflow or by hasen j
Published on 2010-03-24T18:11:23Z Indexed on 2010/03/25 14:53 UTC
Read the original article Hit count: 285

Filed under:
|
|

I'm not very fluent with SQL Server commands.

I need a script to restore a database from a .bak file and move the logical_data and logical_log files to a specific path.

I can do:

restore filelistonly from disk='D:\backups\my_backup.bak'

This will give me a result set with a column LogicalName, next I need to use the logical names from the result set in the restore command:

restore database my_db_name from disk='d:\backups\my_backups.bak' with file=1,
move 'logical_data_file' to 'd:\data\mydb.mdf',
move 'logical_log_file' to 'd:\data\mylog.ldf'

How do I capture the logical names from the first result set into variables that can be supplied to the "move" command?

I think the solution might be trivial, but I'm pretty new to SQL Server.

© Stack Overflow or respective owner

Related posts about sql-server

Related posts about database-restore