tsql error

When I run the following query it works in tsql

RESTORE DATABASE [Temenos] FROM DISK = '\\corpfs03\backups\Temenos\Temenos_backup_2018_05_16_220001_5364522.bak'
with
MOVE 'Akcelerant' TO 'd:\temenos\data\Temenos.mdf',
MOVE 'Akcelerant_Log' TO 'd:\temenos\log\Temenos_log.ldf',
recovery, REPLACE, STATS = 10;


but when I try to make this as a restore job and try to put the same value within this script i am getting errors, any idea what am i doing wrong
USE Master;
GO
SET NOCOUNT ON

-- 1 - Variable declaration
DECLARE @dbName sysname
DECLARE @backupPath NVARCHAR(500)
DECLARE @cmd NVARCHAR(500)
DECLARE @fileList TABLE (backupFile NVARCHAR(255))
DECLARE @lastFullBackup NVARCHAR(500)
DECLARE @lastDiffBackup NVARCHAR(500)
DECLARE @backupFile NVARCHAR(500)

-- 2 - Initialize variables
SET @dbName = 'Temenos'
SET @backupPath = '\\corpfs03\backups\Temenos\'

-- 3 - get list of files
SET @cmd = 'DIR /b "' + @backupPath + '"'

INSERT INTO @fileList(backupFile)
EXEC master.sys.xp_cmdshell @cmd

-- 4 - Find latest full backup
SELECT @lastFullBackup = MAX(backupFile)
FROM @fileList
WHERE backupFile LIKE '%.BAK'
   AND backupFile LIKE @dbName + '%'

SET @cmd = 'RESTORE DATABASE [' + @dbName + '] FROM DISK = ''' + @backupPath + @lastFullBackup + ''' WITH MOVE '[Akcelerant]' TO 'd:\temenos\data\Temenos.mdf', MOVE 'Akcelerant_Log' TO 'd:\temenos\log\Temenos_log.ldf', recovery, REPLACE'

PRINT @cmd
-- check for log backups
DECLARE backupFiles CURSOR FOR
   SELECT backupFile
   FROM @fileList
   --WHERE backupFile LIKE '%.TRN'
   WHERE backupFile LIKE @dbName + '%'
   AND backupFile > @lastFullBackup

OPEN backupFiles

-- Loop through all the files for the database
FETCH NEXT FROM backupFiles INTO @backupFile

WHILE @@FETCH_STATUS = 0
BEGIN
   --SET @cmd = 'RESTORE LOG [' + @dbName + '] FROM DISK = '''
      -- + @backupPath + @backupFile + ''' WITH NORECOVERY'
   --PRINT @cmd
   FETCH NEXT FROM backupFiles INTO @backupFile
END

CLOSE backupFiles
DEALLOCATE backupFiles

-- 6 - put database in a useable state
--SET @cmd = 'RESTORE DATABASE [' + @dbName + '] WITH RECOVERY,REPLACE'
--print @cmd
EXECUTE sp_executesql @cmd


Error:
Msg 102, Level 15, State 1, Line 28
Incorrect syntax near 'Akcelerant'.
Msg 132, Level 15, State 1, Line 28
The label 'd' has already been declared. Label names must be unique within a query batch or stored procedure.
Fay ADBAAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
1) You definitely need to add quotes around the physical filenames in the final command (but no brackets around the logical file names) , and use NORECOVERY not RECOVERY (or you won't be able to apply any logs):

SET @cmd = 'RESTORE DATABASE [' + @dbName + '] FROM DISK = ''' + @backupPath + @lastFullBackup + ''' WITH MOVE ''Akcelerant'' TO ''d:\temenos\data\Temenos.mdf'', MOVE ''Akcelerant_Log'' TO ''d:\temenos\log\Temenos_log.ldf'', NOrecovery, REPLACE'
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fay ADBAAuthor Commented:
That worked, thank you.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Databases

From novice to tech pro — start learning today.