Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在批处理脚本中使用Sql变量_Sql_Sql Server_Batch File_Cmd - Fatal编程技术网

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

在批处理脚本中使用Sql变量,sql,sql-server,batch-file,cmd,Sql,Sql Server,Batch File,Cmd,我已经看了很多关于这方面的文章,但是我无法将这些建议应用到我的研究中。 我希望仅当file1存在且sql命令返回的行计数大于0时,才能运行代码块。两者都必须为true才能运行代码块(:RUNCODE)。我已经设置了许多IF来处理这个问题,它们可以跳转到:RUNCODE或:END SET SqlServerName="SERVER1" SET SqlDatabaseName="DB1" SET SQL="SELECT COUNT(*) FROM TABLE1"

我已经看了很多关于这方面的文章,但是我无法将这些建议应用到我的研究中。 我希望仅当file1存在且sql命令返回的行计数大于0时,才能运行代码块。两者都必须为true才能运行代码块(:RUNCODE)。我已经设置了许多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
(1行受影响)


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 
)
下面是如何在没有空格的情况下获得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
SET SQL=“在dbo.ACCT中选择计数(*)时设置NOCOUNT”
SQLCMD.exe-S%SqlServerName%-E-d%SqlDatabaseName%-Q%SQL%-h-1-o RowCount.txt
对于/f“delims=“%%a in('type Rowcount.txt'),请调用:Trim rcount%%a
如果/i存在RowCount.txt DEL/f/q RowCount.txt
回显%r计数%
退出/b
:修剪
对于/f“令牌=1*”(“%*”)中的%%a,请设置“%%a=%%b”
退出/b

您可以发布脚本的示例输出吗?该文件只有三行。第一个具有行计数(0),并且在结果前面有空白。第二行还有一个空行。最后,“(1行受影响)”位于第三行。无需修剪
RowCount
变量。如果你按照@MattWilliamson的答案来修改你的
()
,你的脚本应该可以很好地工作。@Jon,你是对的。即使在有空白的情况下,它仍然有效。括号是关键问题Hi Matt,你能给我一个这个代码正在做什么的快速分解吗?现在我有点不明白。首先,我在SQL语句中添加了
set nocount on
,这样它就不会打印出
(受影响的1行)
,然后它使用for循环从文件中获取rowcount值,然后调用
:trim
函数来删除数字周围的空白。(for循环发挥了神奇的作用)
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