我必须为训练课恢复几个数据库,并且我正在尝试创建一个脚本,我只需要更改一个变量。这就是我所拥有的,但它不起作用。
-- Declare the variable to be used.
DECLARE @DBVariable char;
-- Initialize the variable.
SET @DBVariable = '01';
USE [master]
RESTORE DATABASE [DBName] + @DBVariable FROM DISK = N'C:\Synergy Manual Backup\DBName.bak' WITH FILE = 1,
MOVE N'SLDemoSystem_Data' TO N'C:\MSSQL\DATA\DBName' + @DBVariable + '.mdf',
MOVE N'SLDemoSystem_Log' TO N'C:\MSSQL\LOG\DBName' + @DBVariable + '.ldf',
NOUNLOAD, STATS = 5
GO
错误消息是:
Msg 102,Level 15,State 1,Line 11
'+'附近的语法不正确。Msg 319,Level 15,State 1,Line 11
关键字'with'附近的语法不正确。如果此语句是公用表表达式,xmlnamespaces子句或更改跟踪上下文子句,则必须以分号结束前一个语句。
答案 0 :(得分:1)
在备份/恢复期间,您需要使用动态sql作为字符串concat,不能使用数据库名称
-- Declare the variable to be used. -- Change it to 2 chars
DECLARE @DBVariable char(2);
DECLARE @SQL NVARCHAR(MAX);
-- Initialize the variable.
SET @DBVariable = '01';
SET @SQL =
N'RESTORE DATABASE [DBName' + @DBVariable '] FROM DISK = N''C:\Synergy Manual Backup\DBName.bak'' WITH FILE = 1,
MOVE N''SLDemoSystem_Data'' TO N''C:\MSSQL\DATA\DBName' + @DBVariable + '.mdf'',
MOVE N''SLDemoSystem_Log'' TO N''C:\MSSQL\LOG\DBName' + @DBVariable + '.ldf'',
NOUNLOAD, STATS = 5'
USE [master]
EXEC sp_executesql @SQL
答案 1 :(得分:1)
您可以使用以下变量:
NSPhotoLibraryUsageDescription
答案 2 :(得分:0)
在执行备份示例之前,请声明并设置变量值,我的示例如下:
SELECT @SQL = ' BACKUP DATABASE '+@bdName
SELECT @SQL += ' TO DISK = '''+@backUpPath+''''
SELECT @SQL += ' WITH FORMAT, '
SELECT @SQL += ' MEDIANAME = '''+@bdName+''''
SELECT @SQL += ' ,NAME = '''+@bdName+''''
PRINT @SQL
EXEC (@SQL)
还原对我有用的示例代码如下:
SELECT @SQL = ' RESTORE DATABASE '+@DEVbdName
SELECT @SQL = ' FROM DISK = '''+@backUpPath+''''
SELECT @SQL = ' WITH RECOVERY,'
SELECT @SQL = ' MOVE '''+@bdName+''' TO '''+@DEVmdlFile+''''
SELECT @SQL = ' ,MOVE '''+@DEVbdLogName+''' TO '+@DEVlogFile+'''';
EXEC (@SQL)