Backup databases whose names includes spaces

fredmutfredmut Posts: 7
edited June 22, 2012 9:08AM in SQL Backup Previous Versions
I need to backup a set of databases (via SQL Agent job) with names that include spaces (yes I know that's not best practice). I'm using a command like EXECUTE master..sqlbackup ''-SQL "BACKUP DATABASES [Bad Database Name].... but it doesn't parse. I've tried surrounding the name in double quotes and square brackets without success. Any help would be greately appreciated.


  • Hi,

    Thanks for your post. When using the extended stored procedure, the parameters must be surrounded by one set of single quotes, so any variables inside the command must be surrounded by two sets of single quotes (or square brackets). If your database name includes spaces, you should surround it with square brackets, rather than two pairs of single quotes. Your command should look like this (replacing variables and WITH options as required):
    execute master..sqlbackup '-sql "BACKUP DATABASE [database name] TO DISK = ''C:\Backups\<AUTO>.sqb'' WITH CHECKSUM, THREADCOUNT = 3" '
    (The disk location is surrounded by two sets of single quotes, whereas the BACKUP command is surrounded by one set of double quotes. The entire parameter is surrounded by one set of single quotes.)

    There is more information about using the extended stored procedure here: and examples of SQL Backup syntax here:

    If this doesn't help, can you post the exact command you are using?


    Marianne Crowder
    Red Gate Software Limited
  • Thanks Marianne. I'll give that a try. One more question - can I pass in a TSQL like variable for the database name so I don't have to maintain a hard coded list of databases to backup or exclude? I'm not finding that addressed in the documentation.
Sign In or Register to comment.