newbieCSharp
newbieCSharp

Reputation: 191

windows batch file executing sql server query

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

Upvotes: 1

Views: 1536

Answers (1)

Gidil
Gidil

Reputation: 4137

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.

Upvotes: 1

Related Questions