0

I have looked at a number of threads covering this but am unable to apply the advice to my stituation. I want to be able to run a block of code only if file1 exists and the row count from a sql command return a count greater than 0. Both must be true to run the code block (:RUNCODE). I have set up a number of IF's to handle this which jump to either :RUNCODE or :END.

    SET SqlServerName="SERVER1"
    SET SqlDatabaseName="DB1"
    SET SQL="SELECT COUNT(*) FROM TABLE1" 

    SQLCMD.exe -S%SqlServerName% -E -d%SqlDatabaseName%  -Q%SQL% -h -1 -o RowCount.txt

    SET /p RowCount= <RowCount.txt
    DEL RowCount.txt

    echo %RowCount%


    IF EXIST "File1" IF %RowCount% GTR 0 
    (
            ECHO Running the position Loader for EFA...
            GOTO RUNCODE
     )


    IF NOT EXIST "File1" IF %RowCount% EQU 0
    (              
            ECHO The Position File Does not Exist...
            GOTO END
    )

    IF EXIST "File1" IF %RowCount% EQU 0
    (
            ECHO The File Does not Exist...
            GOTO END
    )

    IF NOT EXIST "File1" IF %RowCount% GTR 0
    (
            ECHO The Position File Does not Exist...
            GOTO END 
    )

    :RUNCODE 
    echo Running code block now now...
    EXIT

    :END
    echo conditions not met...
    EXIT

The code above gives me RowCount=0 (with white space in the variable) and appears to completely skip the if conditions.

Any help would be greatly appreciated.

Example of the ouput script. Note that there is white space in front of the 0 and not in front of (1 row affected)

      0

(1 row affected)

4
  • Can you post an example output of your script? Commented Feb 28, 2014 at 14:21
  • The file is just three lines. First has the row count (0) and has white space in front of the result. Then there is an empty line for line two. Finally '(1 row affected)' is on the third line. Commented Feb 28, 2014 at 14:26
  • You don't need to trim your RowCount variable. If you follow @MattWilliamson's answer and fix your (), your script should work fine. Commented Feb 28, 2014 at 15:05
  • @Jon, you were right. It still worked even with the white space. The brackets were the key issue Commented Mar 7, 2014 at 15:40

1 Answer 1

2

Your opening parenthesis for the IF's need to be on the same line.

IF EXIST "File1" IF %RowCount% GTR 0 (
        ECHO Running the position Loader for EFA...
        GOTO RUNCODE
)


IF NOT EXIST "File1" IF %RowCount% EQU 0 (              
        ECHO The Position File Does not Exist...
        GOTO END
)

IF EXIST "File1" IF %RowCount% EQU 0 (
        ECHO The File Does not Exist...
        GOTO END
)

IF NOT EXIST "File1" IF %RowCount% GTR 0 (
        ECHO The Position File Does not Exist...
        GOTO END 
)

Here is how you can get rowcount without the whitespace

SET SQL="SET NOCOUNT on SELECT COUNT(*) FROM dbo.ACCT" 

SQLCMD.exe -S%SqlServerName% -E -d%SqlDatabaseName%  -Q%SQL% -h -1 -o RowCount.txt
for /f "delims=" %%a in ('type Rowcount.txt') do Call :Trim rcount %%a
if /i exist RowCount.txt DEL /f /q RowCount.txt
echo %rcount%
exit /b

:Trim <return> <string>
for /f "tokens=1*" %%a in ("%*") do set "%%a=%%b"
exit /b
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Matt, could you give me a quick break down of what this code is doing? Its all a bit beyond me at the moment.
well, first I added set nocount on to the SQL statement so it doesn't print out (1 row affected) Then it uses a for loop to get the rowcount value out of the file and then the :trim function is called to remove the whitespace around the number. (the for loop does the magic)

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.