I am having some issues with the syntax. Can you point out where I am going wrong?
I am getting the following error Syntax error: 'fjfelej!'' after '\\kprod-nas03\sqlbackups\FULL_DB009_superbill_2039_prod_20140321_112322_encr.sqb WITH PASSWORD='
[code]
Create Table #backupFiles(ID Int Identity(1,1),FileName Varchar(128), depth int, isfile int, databasename varchar(64))
Insert into #backupfiles(FileName, depth, isfile)
EXEC master.sys.xp_dirtree '\\kprod-nas03\ServerUpdates\SQLRestore\',0,1;
Update #backupfiles
set databasename=
substring(FileName,12,len(filename)-charindex('2014',filename)-case when len(filename)=55 then 4 else 3 end)
Create TAble #Restore(ID Int Identity(1,1),FileName Varchar(128), DatabaseName varchar(64))
Insert into #Restore(FileName, DatabaseName)
select FileName,Databasename
from #backupfiles
where filename like '%superbill%'
Declare @DbCount int
Declare @Counter Int
DECLARE @exitcode int
DECLARE @sqlerrorcode int
DECLARE @sitename nvarchar(128)
DECLARE @sql nvarchar(1024)
Declare @DatabaseName varchar(64)
Set @DBCount=(Select Max(ID) from #Restore)
SET @counter=(Select Min(ID) From #Restore)
While @Counter<=@DBCount
Begin
SET @sitename = (Select FileName from #Restore where ID =@Counter)
Set @DatabaseName= (Select DatabaseName from #Restore where ID =@Counter)
SET @sql = '-SQL "RESTORE DATABASE ' + @databasename + ' FROM DISK = ''\\kprod-nas03\sqlbackups\' + @sitename + ' WITH PASSWORD=''fjfelejl'' SOURCE = ''' + @DatabaseName + '''
WITH MOVE DATAFILES TO ''D:\SQLData'' , MOVE LOGFILES TO ''D:\SQLLogs'' ,
MAILTO = ''pam.ozer@kareo.com'', RECOVERY,
CHECKDB = ''ALL_ERRORMSGS, DATA_PURITY, EXTENDED_LOGICAL_CHECKS'''
EXEC master..sqlbackup @sql, @exitcode OUT, @sqlerrorcode OUT
Set @Counter=@Counter+1
END
----
drop table #backupFiles, #Restore
[/code]
I am getting the following error Syntax error: 'fjfelej!'' after '\\kprod-nas03\sqlbackups\FULL_DB009_superbill_2039_prod_20140321_112322_encr.sqb WITH PASSWORD='
[code]
Create Table #backupFiles(ID Int Identity(1,1),FileName Varchar(128), depth int, isfile int, databasename varchar(64))
Insert into #backupfiles(FileName, depth, isfile)
EXEC master.sys.xp_dirtree '\\kprod-nas03\ServerUpdates\SQLRestore\',0,1;
Update #backupfiles
set databasename=
substring(FileName,12,len(filename)-charindex('2014',filename)-case when len(filename)=55 then 4 else 3 end)
Create TAble #Restore(ID Int Identity(1,1),FileName Varchar(128), DatabaseName varchar(64))
Insert into #Restore(FileName, DatabaseName)
select FileName,Databasename
from #backupfiles
where filename like '%superbill%'
Declare @DbCount int
Declare @Counter Int
DECLARE @exitcode int
DECLARE @sqlerrorcode int
DECLARE @sitename nvarchar(128)
DECLARE @sql nvarchar(1024)
Declare @DatabaseName varchar(64)
Set @DBCount=(Select Max(ID) from #Restore)
SET @counter=(Select Min(ID) From #Restore)
While @Counter<=@DBCount
Begin
SET @sitename = (Select FileName from #Restore where ID =@Counter)
Set @DatabaseName= (Select DatabaseName from #Restore where ID =@Counter)
SET @sql = '-SQL "RESTORE DATABASE ' + @databasename + ' FROM DISK = ''\\kprod-nas03\sqlbackups\' + @sitename + ' WITH PASSWORD=''fjfelejl'' SOURCE = ''' + @DatabaseName + '''
WITH MOVE DATAFILES TO ''D:\SQLData'' , MOVE LOGFILES TO ''D:\SQLLogs'' ,
MAILTO = ''pam.ozer@kareo.com'', RECOVERY,
CHECKDB = ''ALL_ERRORMSGS, DATA_PURITY, EXTENDED_LOGICAL_CHECKS'''
EXEC master..sqlbackup @sql, @exitcode OUT, @sqlerrorcode OUT
Set @Counter=@Counter+1
END
----
drop table #backupFiles, #Restore
[/code]