Reputation: 191
I have written the batch file to print out max(ID) from sql server. I see for loop, looping through twice, once printing the correct iD number and in next iteration prints a bracket '('. I have no clue why it is iterating twice.
@echo on
set maxid=0
echo %maxid%
timeout /t 10
for /F "usebackq tokens=1" %%i in (`sqlcmd -E -S "DBNAME\instance01" -h-1 -Q "select max(ID) from table1"`) do set maxid=%%i
echo testingcount
echo %maxid%
timeout /t 10
if %maxid% NEQ 0 (
echo count not zero
echo %maxid%
timeout /t 10
goto end
) else if %maxid% EQU 0(
echo count is zero
timeout /t 10
}
:end
echo END
Thanks
Upvotes: 0
Views: 1489
Reputation: 44881
I think you might have been getting a part of the output that you didn't want ((1 row(s) affected)
. Try this instead:
@echo off
set maxid=0
echo %maxid%
timeout /t 10
for /F "usebackq tokens=1" %%i in (`sqlcmd -E -S "DBNAME\instance01" -h-1 -Q "set nocount on;select max(id) from table1"`) do set maxid=%%i
echo testingcount
echo %maxid%
timeout /t 10
if %maxid% GTR 0 (
echo count not zero
echo %maxid%
timeout /t 10
goto end
) else (
if %maxid% EQU 0 (
echo count is zero
timeout /t 10
)
)
:end
echo END
Upvotes: 1