Comments
4 comments
-
The entire command needs to be in a single string value e.g.
DECLARE @cmd NVARCHAR(1024) DECLARE @exitcode INT DECLARE @errorcode INT DECLARE @dt CHAR(8) SET @dt = CONVERT(CHAR(8),GETDATE(),112) SET @cmd = N'-SQL "RESTORE DATABASE [CaliforniaSE] FROM DISK = ''\\principal\backups\database\FULL_(local)_California_' + @dt + '_030000.sqb'' WITH RECOVERY, MOVE ''California'' TO ''F:\FileGroups\DataSE\CaliforniaSE.mdf'', MOVE ''California _Data'' TO ''F:\FileGroups\DataSE\CaliforniaSE_Data.ndf'', MOVE ''California_Indexes'' TO ''G:\FileGroups\IndexesSE\CaliforniaSE_Indexes.ndf'', MOVE ''California_Log'' TO ''H:\FileGroups\LogsSE\CaliforniaSE_Log.ldf''"' EXEC master..sqlbackup @cmd, @exitcode OUTPUT, @errorcode OUTPUT
-
Peter,
Thanks for the help. Just to complete the loop, since I've run the command once and created the target db, I need to replace
WITH RECOVERY with WITH RECOVERY, REPLACE
yes? -
If using SQL Server 2005, yes.
-
Fixed syntax errors and took seconds off default <AUT0> name, so we've got this, which works:
DECLARE @cmd NVARCHAR(1024) DECLARE @exitcode INT DECLARE @errorcode INT DECLARE @dt CHAR(8) SET @dt = CONVERT(CHAR(8),GETDATE(),112) SET @cmd = '''N -SQL "RESTORE DATABASE [CaliforniaSE] FROM DISK = ''\\principal\backups\database\FULL_(local)_California_' + @dt + '_0300.sqb'' WITH MOVE ''California'' TO ''F:\FileGroups\DataSE\CaliforniaSE.mdf'', MOVE ''California _Data'' TO ''F:\FileGroups\DataSE\CaliforniaSE_Data.ndf'', MOVE ''California_Indexes'' TO ''G:\FileGroups\IndexesSE\CaliforniaSE_Indexes.ndf'', MOVE ''California_Log'' TO ''H:\FileGroups\LogsSE\CaliforniaSE_Log.ldf''" WITH RECOVERY, REPLACE' PRINT @cmd EXEC master..sqlbackup @cmd, @exitcode OUTPUT, @errorcode OUTPUT
Add comment
Please sign in to leave a comment.
I need to run sqlbackup nightly to restore a backup using a dynmically constructed string inserting a date in the <AUTO> name string as below so that I can automate the restore process on a secondary server. The backup is from an EE instance and the restore is to an SE instance. What I have below errors out where I try to insert + @dt + into the command. How do I do this? This command, pulled from the script tab and altered to use the correct paths, succeeds with the date hardcoded, so I'm close, I guess.