Skip to content Skip to sidebar Skip to footer

How To Set A Sqlcmd Output To A Batch Variable?

I'm trying to set the output of a sqlcmd query to a variable in a batch file. Here's my query: sqlcmd -S -d -Q 'select max(Column1)+1 from Table1'

Solution 1:

Try turning on NOCOUNT:

for /f %%a in ('sqlcmd -S <SERVER> -d <DATABASE> -Q "SET NOCOUNT ON; select max(Column1)+1 from Table1"') do set ColumnVar=%%a
echo %ColumnVar%
pause

Post a Comment for "How To Set A Sqlcmd Output To A Batch Variable?"