In a Windows batch file, this works:
sqlcmd.exe -b -S xxMySqlServerNamexx -Q "BACKUP DATABASE xxMyDatabaseNamexx TO DISK='d:\data\xxMyDatabaseNamexx.bak' with init, compression"
As does this:
set vSource_SqlServer="xxMySqlServerNamexx"
sqlcmd.exe -b -S %vSource_SqlServer% -Q "BACKUP DATABASE xxMyDatabaseNamexx TO DISK='d:\data\xxMyDatabaseNamexx.bak' with init, compression"
But this:
set vSource_SqlServer="xxMySqlServerNamexx"
set vSource_SqlDbName="xxMyDatabaseNamexx"
sqlcmd.exe -b -S %vSource_SqlServer% -Q "BACKUP DATABASE %vSource_SqlDbName% TO DISK='d:\data\xxMyDatabaseNamexx.bak' with init, compression"
...causes error:
Sqlcmd: 'xxMyDatabaseNamexx" TO DISK='d:\data\xxMyDatabaseNamexx.bak' with init, compression"': Unexpected argument. Enter '-?' for help.
As you can see it is choking on using a variable %vSource_SqlDbName% in place of xxMyDatabaseNamexx
Is there a correct way to do this in this form (by that I mean, yes I should probably be using Powershell or an alternative approach, but I have several existing batch files in this form that I would prefer to convert to use variables in this form, even if it is not the perfect way to do it)?