![](/img/trans.png)
[英]OPENJSON() returns 0 rows from SQLCMD, but returns many rows in SSMS
[英]Why does the following sqlcmd call returns 0 rows when in a bat file? (passing manually)
我在编写批处理脚本时遇到麻烦。 我将范围缩小到似乎可以打破的地方,并提供了一个示例。 将以下代码粘贴到控制台中时,将返回10行:
set TESTRUNID=111222
set QUERY="select distinct col1 from Table where col2='%TESTRUNID%' and col3 LIKE '%es'"
start /B /wait sqlcmd -S dahost -U usr -P pwd -Q %QUERY% -o resfile.txt
当我将其放入批处理脚本中时,它将返回0行!
@echo off
setlocal EnableDelayedExpansion
REM remark
REM remark
set TESTRUNID=111222
set QUERY="select distinct col1 from Table where col2='%TESTRUNID%' and col3 LIKE '%es'"
start /B /wait sqlcmd -S dahost -U usr -P pwd -Q %QUERY% -o resfile.txt
我认为您正在混淆使用百分号来表示(1)批处理变量扩展和(2)SQL通配符。 在批处理文件中,对SQL通配符使用双%%符号:
set QUERY="select distinct col1 from Table where col2='%TESTRUNID%' and col3 LIKE '%%es'"
在将双%符号传递给SQLCMD之前,已将其转换为单%符号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.