It looks like the previous posters have fixed your problem, but this may have been avoided if you had used dynamic sql in the 'best practice' manner. Concatenating the string together as a mixture of variables and string literals is not ideal as it makes working with apostrophes difficult (as shown here).
A better way is to write your sql as
declare @DBname nvarchar(255) = 'testdb'
,@BakName nvarchar(255) = 'C:loc_smartz_db0_template.bak'
,@MovemdfName nvarchar(255) = 'C:Program FilesMicrosoft SQL ServerMSSQLDataTestDatabase1.mdf'
,@MoveldfName nvarchar(255) = 'C:Program FilesMicrosoft SQL ServerMSSQLDataTestDatabase1.ldf'
,@sqlcommand nvarchar(max)
,@paramList nvarchar(max)
set @paramList = '@DBname nvarchar(255), @BakName nvarchar(255), @MovemdfName nvarchar(255), @MoveldfName nvarchar(255)'
set @sqlcommand = N'Restore DATAbase @DBname from disk = @BakName with move @DBname to @MovemdfName, move @DBname to @MoveldfName, Replace'
exec sp_executesql @statement = @sqlcommand
,@params = @paramList
,@DBname = @DBname
,@BakName = @BakName
,@MovemdfName = @MovemdfName
,@MoveldfName = @MoveldfName
This way, your sql command is very easy to read and maintain. Note that you don't have to mess around with escaping the apostrophes in the variable values either if you have spaces in your pathnames.
It also has the advantage (if you have the code in a stored proc) of allowing SQL Server to reuse execution plans which will improve performance.
See here for more information.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…