title: How to set a sqlcmd output to a batch variable?
body: <p>I'm trying to set the output of a sqlcmd query to a variable in a batch file. </p>
  
  <p>Here's my query: </p>
  
  <pre><code>sqlcmd -S &lt;SERVER&gt; -d &lt;DATABASE&gt; -Q "select max(Column1)+1 from Table1"
  </code></pre>
  
  <p>This gives me exactly what I would expect and what I want: </p>
  
  <pre><code>-----------
           10
  &lt;1 rows affected&gt;
  </code></pre>
  
  <p>However, when I try to set it to a variable, I used this script:</p>
  
  <pre><code>for /f %%a in ('sqlcmd -S &lt;SERVER&gt; -d &lt;DATABASE&gt; -Q "select max(Column1)+1 from Table1"') 
      do set ColumnVar=%%a
  echo %ColumnVar%
  pause
  </code></pre>
  
  <p>This gives me this result instead: <code>&lt;1 rows affected&gt;</code> I'm guessing this is because the loop is setting the variable to the last line. So is there a way I could use tokens and delims to parse out the 10 instead?</p>