Add date to SQL database backup filename

前端 未结 7 565
旧时难觅i
旧时难觅i 2021-02-01 14:03

I\'m using the below to backup a db from a SQL job. Can someone tell me how to add the current date to the output filename? Preferably in YYYYMMDD format.

BACK         


        
相关标签:
7条回答
  • 2021-02-01 14:34

    Use the following

    DECLARE @BackupFileName varchar(20)
    
    SELECT @BackupFileName = '\\ServerName\SharedFolder\DatabaseName_' + CONVERT (VarChar, GetDate(), 112) + '.bak'
    
    BACKUP DATABASE [myDB] TO  DISK = @BackupFileName WITH NOFORMAT, INIT,  NAME = N'myDB', SKIP, REWIND, NOUNLOAD,  STATS = 10
    

    Read up on Cast and Convert here http://msdn.microsoft.com/en-us/library/ms187928.aspx

    0 讨论(0)
提交回复
热议问题