title: Batch - SQLCMD with Parameters not working in for loop
body: <p>I am creating a batch script that loops through all the results from an SQL result set returned via SQLCMD</p>
  
  <p>I have done this before with no problems, however the difference this time is that the SQL script accepts a parameter and this seems to be what is causing problems</p>
  
  <p>If I create a batch file with the following code, the statement returns the records as expected:</p>
  
  <pre><code>sqlcmd -S MyServer\MyInstance -d MyDatabase -U sa -P MyPassword -i C:\MySQLFile.sql -v Surname="Smith"
  </code></pre>
  
  <p>This returns all the records where surname is Smith to console window. All OK so far.</p>
  
  <p>However, lets say I want to do something for each record returned in the FOR loop</p>
  
  <pre><code>FOR /f "tokens=1" %%i IN ('sqlcmd -S MyServer\MyInstance -d MyDatabase -U sa -P MyPassword -i C:\MySQLFile.sql -v Surname="Smith"') DO (
      echo hello
  )
  </code></pre>
  
  <p>I get </p>
  
  <pre><code>Sqlcmd: 'Smith': Invalid argument. Enter '-?' for help.
  </code></pre>
  
  <p>If i change the SQL script so it has no parameters, I can do the same (but remove -v and it works:</p>
  
  <pre><code>FOR /f "tokens=1" %%i IN ('sqlcmd -S MyServer\MyInstance -d MyDatabase -U sa -P MyPassword -i C:\MySQLFile.sql) DO (
  echo hello
  )
  </code></pre>
  
  <p>Not sure if there is some special way I need to deal with the SQL CMD parameters inside the FOR loop?</p>
  
  <p>PS, this problem has been anonymised for post on here, I am obviously not really writing a program that writes "hello" for every Smith in a database!</p>