Monday, March 19, 2012

DB Restore

I often have to restore databases from backups sent to me by clients.
I do a
RESTORE FILELISTONLY FROM DISK='c:\db.bak'
which gives me the data and log file names, say 'data_file' and
'data_file_log'
and then a
RESTORE DATABASE DbName
FROM DISK = 'c:\db.bak'
WITH MOVE 'data_file' TO 'c:\test\db.mdf',
MOVE 'data_file_log' TO 'c:\test\db.ldf'
Does anyone know a way that I can read the names of the data and log files
into variables
in the 'RESTORE FILELISTONLY' command and then substitute them
into the 'RESTORE DATABASE' command ?
TIA
SteveYou should be able to do like below:
CREATE TABLE #FileDetails(...)
INSERT #FileDetails(col1, col2...)
EXEC('RESTORE FILELISTDETAILS...')
Now you can read the stuff off of that table and get the stuff into TSQL
variables. And then use those variables in the RESTORE command. In case SQL
Server doesn't accepts variables in RESTORE for the relevant option, you can
use dynamic SQL to execute the RESTORE command.
--
Tibor Karaszi, SQL Server MVP
Archive at:
http://groups.google.com/groups?oi=djq&as_ugroup=microsoft.public.sqlserver
"Steve W" <lsl@.btconnect.com.no_spam> wrote in message
news:OZXew4N7DHA.2404@.TK2MSFTNGP12.phx.gbl...
> I often have to restore databases from backups sent to me by clients.
> I do a
> RESTORE FILELISTONLY FROM DISK='c:\db.bak'
> which gives me the data and log file names, say 'data_file' and
> 'data_file_log'
> and then a
> RESTORE DATABASE DbName
> FROM DISK = 'c:\db.bak'
> WITH MOVE 'data_file' TO 'c:\test\db.mdf',
> MOVE 'data_file_log' TO 'c:\test\db.ldf'
> Does anyone know a way that I can read the names of the data and log files
> into variables
> in the 'RESTORE FILELISTONLY' command and then substitute them
> into the 'RESTORE DATABASE' command ?
> TIA
> Steve
>

No comments:

Post a Comment