SQL 文件名中的日期
SQL date in file name
我正在尝试通过脚本进行备份
BACKUP DATABASE [MITxxxx] TO DISK = N'C:\Program Files (x86)\Mobile Inventory System\MITxxx' WITH NOFORMAT, NOINIT, NAME = N'MITXXX-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
以上查询工作正常,但我想在文件名中添加日期,如
BACKUP DATABASE [MITXXX] TO DISK = N'C:\Program Files (x86)\Mobile Inventory System\MITXXXX_'+ GETDATE() WITH NOFORMAT, NOINIT, NAME = N'MITXXX-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
但我收到错误消息
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '+'. Msg 319,
Level 15, State 1, Line 1 Incorrect syntax near the keyword 'with'. If
this statement is a common table expression, an xmlnamespaces clause
or a change tracking context clause, the previous statement must be
terminated with a semicolon.
我在自动备份脚本中做了非常相似的事情:
DECLARE @dateDrive VARCHAR(64)
SET @dateDrive=FORMAT(GETDATE(), 'yyMMdd')
DECLARE @SQLString nvarchar(500);
SET @SQLString=
N'BACKUP DATABASE [DatabaseName]
TO DISK=N''W:\Database Backups\AutoBackups\DatabaseName{DateDrive}.bak''
WITH INIT
, NAME=N''DatabaseName-Backup''
, NOFORMAT, SKIP, NOREWIND, NOUNLOAD, STATS=10'
SET @SQLString=REPLACE(@SQLString, '{DateDrive}', @dateDrive)
EXECUTE sp_executesql @SQLString
我认为这里的关键行是 SET @dateDrive=FORMAT(GETDATE(), 'yyMMdd').
我正在尝试通过脚本进行备份
BACKUP DATABASE [MITxxxx] TO DISK = N'C:\Program Files (x86)\Mobile Inventory System\MITxxx' WITH NOFORMAT, NOINIT, NAME = N'MITXXX-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
以上查询工作正常,但我想在文件名中添加日期,如
BACKUP DATABASE [MITXXX] TO DISK = N'C:\Program Files (x86)\Mobile Inventory System\MITXXXX_'+ GETDATE() WITH NOFORMAT, NOINIT, NAME = N'MITXXX-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, STATS = 10
GO
但我收到错误消息
Msg 102, Level 15, State 1, Line 1 Incorrect syntax near '+'. Msg 319, Level 15, State 1, Line 1 Incorrect syntax near the keyword 'with'. If this statement is a common table expression, an xmlnamespaces clause or a change tracking context clause, the previous statement must be terminated with a semicolon.
我在自动备份脚本中做了非常相似的事情:
DECLARE @dateDrive VARCHAR(64)
SET @dateDrive=FORMAT(GETDATE(), 'yyMMdd')
DECLARE @SQLString nvarchar(500);
SET @SQLString=
N'BACKUP DATABASE [DatabaseName]
TO DISK=N''W:\Database Backups\AutoBackups\DatabaseName{DateDrive}.bak''
WITH INIT
, NAME=N''DatabaseName-Backup''
, NOFORMAT, SKIP, NOREWIND, NOUNLOAD, STATS=10'
SET @SQLString=REPLACE(@SQLString, '{DateDrive}', @dateDrive)
EXECUTE sp_executesql @SQLString
我认为这里的关键行是 SET @dateDrive=FORMAT(GETDATE(), 'yyMMdd').