-3

I am creating a job which takes backup of a database and with the filename suffixing today's date. But I'm getting below error.

DECLARE @Query NVARCHAR(MAX)
SET @Query = 'BACKUP DATABASE [DBName] TO DISK = ''D:\Backups\DBName_'+cast(convert(date,getdate())as varchar)+'.bak'
EXEC sp_executeSql @Query

Error Message:

Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string 'D:\Backups\DBName_2017-07-07.bak'.
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'D:\Backups\DBName_2017-07-07.bak'.

2 Answers2

1

You are missing the last closing quote. It should end in .txt'''

SET @Query = 'BACKUP DATABASE [MetaCost] TO DISK = ''D:\Backups\DBName.bak_'+cast(convert(date,getdate())as varchar)+'.txt'''

Also typically you use the .bak extension for a sql database backup.

Igor
  • 60,821
  • 10
  • 100
  • 175
0

Your statement will output

BACKUP DATABASE [DBName] TO DISK = 'D:\Backups\DBName.bak_2017-07-07.txt

You need to add '.txt'''

huMpty duMpty
  • 14,346
  • 14
  • 60
  • 99