0

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

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.