Search code examples
batch-filecmdsqlcmd

Incorrect syntax near '&' - How to escape it


Sql backup command syntax error in batch file due to special character '&' in DATABASE name.

What I tried:
test^&2019
'test&2019'
'test^&2019'

sqlcmd -S .\sqlexpress -E -Q "BACKUP DATABASE test&2019 TO DISK='C:\test\test&2019.bak' with init"

Solution

  • Looks like enclosing db name into square braces would work. & escaping inside double quote string is not needed.

    sqlcmd -S .\sqlexpress -E -Q "BACKUP DATABASE [test&2019] TO DISK='C:\test\test&2019.bak' with init"