我需要使用日期备份数据库名称

I need to take database back up database name with date

我需要进行每日数据库备份并将日期添加到数据库名称中 - 如下所示:Navigate_2017-10-20.bak.

如何使用带有数据库名称和日期的动态查询?

DECLARE @Date DATE = GETDATE()

SELECT @Date

BACKUP DATABASE [Navigate] 
TO DISK = N'D:\Working\Navigate_'+ @Date +'.bak' 
WITH NOFORMAT, NOINIT,  
NAME = N'Navigate-Full Database Backup', 
SKIP, NOREWIND, NOUNLOAD, COMPRESSION,  STATS = 10
GO
DECLARE @Date DATE = GETDATE()

SELECT @Date

BACKUP DATABASE [Navigate] 
TO  DISK = N'D:\Working\Navigate_'+ CONVERT(NVARCHAR(20),@Date,105) +'.bak' WITH NOFORMAT, NOINIT,  
NAME = N'Navigate-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION,  STATS = 10
GO

使用这个

DECLARE @Date NVARCHAR(20) = CONVERT(NVARCHAR(20),GETDATE(),105)
SELECT @Date
BACKUP DATABASE [Navigate] 
TO  DISK = N'D:\Working\Navigate_'+ @Date +'.bak' WITH NOFORMAT, NOINIT,  
NAME = N'Navigate-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION,  STATS = 10

将日期转换为字符串并预先创建文件名:

DECLARE @Filename NVARCHAR(MAX);

SELECT @FILENAME = N'D:\Working\Navigate_'+ CONVERT(char(10), GetDate(),126) +'.bak';


BACKUP DATABASE [Navigate] 
TO  DISK = @FILENAME WITH NOFORMAT, NOINIT,  
NAME = N'Navigate-Full Database Backup', SKIP, NOREWIND, NOUNLOAD, COMPRESSION,  STATS = 10;
GO