如何在sqlcmd中使用IF语句
How to use IF statement in sqlcmd
如果在 运行 过程中出现错误 then echo 'error' else 'success'
sqlcmd -S DANAA-LAPTOP -d NewsWebsite -q "SP_DictionaryMain 'NewsWebsite','NEW'"
-o C:\logFile\ReportExE-%date:~-4,4%%date:~-10,2%%date:~-7,2%.txt
try cmd &&
和 ||
运算符,其中 &&
表示前一个命令成功,||
表示前一个命令失败。
如
mycommand && echo success || echo error
你也可以在括号内编写复合命令
mycommand && (set "somevar=ok" & echo success) || (set "somevar=ko" & echo error)
或者,如果您是 运行 批处理脚本
mycommand && (
set "somevar=ok"
echo success
) || (
set "somevar=ko"
echo error
)
Also, you need to use the -b
switch to force sqlcmd
return an
ERRORLEVEL
that may be captured within batch or command line. This switch also forces sqlcmd
to close when an error occurs.
所以,在你的情况下
sqlcmd -b -S DANAA-LAPTOP -d NewsWebsite -q "SP_DictionaryMain 'NewsWebsite','NEW'" -o C:\logFile\ReportExE-%date:~-4,4%%date:~-10,2%%date:~-7,2%.txt 2>NUL && echo success || echo error
如你所愿echo
,错误输出被2>NUL
抑制。删除它以查看来自 sqlcmd
的错误消息
如果在 运行 过程中出现错误 then echo 'error' else 'success'
sqlcmd -S DANAA-LAPTOP -d NewsWebsite -q "SP_DictionaryMain 'NewsWebsite','NEW'"
-o C:\logFile\ReportExE-%date:~-4,4%%date:~-10,2%%date:~-7,2%.txt
try cmd &&
和 ||
运算符,其中 &&
表示前一个命令成功,||
表示前一个命令失败。
如
mycommand && echo success || echo error
你也可以在括号内编写复合命令
mycommand && (set "somevar=ok" & echo success) || (set "somevar=ko" & echo error)
或者,如果您是 运行 批处理脚本
mycommand && (
set "somevar=ok"
echo success
) || (
set "somevar=ko"
echo error
)
Also, you need to use the
-b
switch to forcesqlcmd
return anERRORLEVEL
that may be captured within batch or command line. This switch also forcessqlcmd
to close when an error occurs.
所以,在你的情况下
sqlcmd -b -S DANAA-LAPTOP -d NewsWebsite -q "SP_DictionaryMain 'NewsWebsite','NEW'" -o C:\logFile\ReportExE-%date:~-4,4%%date:~-10,2%%date:~-7,2%.txt 2>NUL && echo success || echo error
如你所愿echo
,错误输出被2>NUL
抑制。删除它以查看来自 sqlcmd