简体   繁体   English

在批处理脚本中使用Sql变量

[英]Using an Sql variable in a batch script

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). 我希望只有当file1存在且sql命令的行计数返回大于0的计数时才能运行代码块。两者都必须为true才能运行代码块(:RUNCODE)。 I have set up a number of IF's to handle this which jump to either :RUNCODE or :END. 我设置了一些IF来处理它,它跳转到:RUNCODE或: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. 上面的代码给了我RowCount = 0(在变量中有空格)并且似乎完全跳过if条件。

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行受影响)

  0 

(1 row affected) (1排受影响)

Your opening parenthesis for the IF's need to be on the same line. IF的左括号需要在同一行。

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 以下是如何在没有空格的情况下获取rowcount的方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM