1

I have a batch file that should execute a sql server query and check if count = 0 or more. if 0 exit if not I run another .exe program. When I run this, the if statement "if ct EQU 0" is not working correctly. In my table count is zero.

@ECHO OFF
SET Header=-----------------------------------------------------
set ct=-1
sqlcmd -S OURDATA\DEVINSTANCE -E -q "SELECT ct=count(*) FROM [dbo].[Table] where Name like '%AMY%' and status=1"
if ct EQU 0 (
echo no count)
goto end
)
if ct GRT 0 (
start "" "C:\test.exe"
if %ERRORLEVEL% NEQ 0 (
echo %header%
Echo There has been an error.
pause
goto end)
)
:end
echo %header%
echo END

Thanks R

1 Answer 1

1

Try replacing it with something like this:

for /F usebackq %%i in (`sqlcmd -E -S OURDATA\DEVINSTANCE -h-1  -Q "SELECT count(*) FROM [dbo].[Table] where Name like '%AMY%' and status=1"`) do (
    set count=%%i
)

Hope this helps you.

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.