We had a deployment with a variable "$(TableName)" that was only mentioned in comments, but we still had the error
2013-07-12 14:38:38 INFO [Deploy Script] ERROR: 'TableName' scripting variable not defined.
coming from the sqlcmd.exe step. Removing the references to it from the comments fixed the problem, but I'm very surprised that commented out variables matter!
It does seem a bit odd that SQLCMD would validate variables within comments, since obviously there is no functional difference between variable name and its value.
The only reason I can think of for this is in the situation where comments are being included in a stored procedure, since SQL Server will include the comment along with the procedure code itself within the system catalogs. Someone may want to pass in a value to go into that comment, but again functionally there is no difference.
You may have to put a placeholder character between the “$” and the variable name to get around this, eg. — $ (VariableName)
This question has received the maximum number of answers.