Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.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:在where子句中使用预定义列表_Sql_Sql Server_String_Groovy_Where - Fatal编程技术网

SQL:在where子句中使用预定义列表

SQL:在where子句中使用预定义列表,sql,sql-server,string,groovy,where,Sql,Sql Server,String,Groovy,Where,下面是我正在尝试做的一个例子: def famlist = selection.getUnique('Family_code') ... Where “””... and testedWaferPass.family_code in $famlist “””... famlist是一个对象列表 “选择”将在每次运行时更改,因此列表总是在更改 我只想返回SQL搜索中的列,其中该行位于我创建的列表中 我意识到它应该是这样的:in('foo','bar'))

下面是我正在尝试做的一个例子:

        def famlist = selection.getUnique('Family_code')

   ... Where “””...
    and testedWaferPass.family_code in $famlist
    “””...
famlist是一个对象列表

“选择”将在每次运行时更改,因此列表总是在更改

我只想返回SQL搜索中的列,其中该行位于我创建的列表中

我意识到它应该是这样的:in('foo','bar'))

但无论我做什么,我的清单都不会变成这样。所以我必须把我的列表变成一个字符串

('\${famlist.join("', '")}')

我试过上面的方法,idk。不是为我工作。我只是想把它扔进去。我想听听你的建议。谢谢。

我愿意打赌,有一种比下面所示更好的方法来实现这一点——但这是可行的。下面是我的示例脚本的重要部分。namelistoriginal包含字符串名称。需要引用列表中的每个条目,然后从toString结果中选择[and]。我尝试将其作为预处理语句传递,但为此,您需要为?对于列表中的每个元素。这种快速攻击不使用预先准备好的语句

def nameList = ['Reports', 'Customer', 'Associates']
def nameListString = nameList.collect{"'${it}'"}.toString().substring(1)
nameListString = nameListString.substring(0, nameListString.length()-1)

String stmt = "select * from action_group_i18n where name in ( $nameListString)"
db.eachRow( stmt ) { row ->
    println  "$row.action_group_id, $row.language, $row.name"
} 

希望这有帮助

您可以使用
FOREACH
循环构建列表,在循环中,您还可以添加变量项。在第一次添加
之前和每次获取
之后以及最后一项添加
。你想用什么语言来完成这个任务?Groovy是我用来完成脚本主要部分的语言。它中间有一个SQL查询。所以,如果我做一个foreach,那么每个部分会是什么?每个部分都会是
famlist
中的一个项目,但我不确定Groovy是如何处理的。您需要询问熟悉Groovy的人。将语句或至少查询本身添加到问题中。哇,这实际上非常有效。唯一的问题是,我需要在每个字符串的开头和结尾都有一个括号,其中'13274SC2001.000','13264SC2008.000'中的TestedWarPass.vendor_lot_id需要在('13274SC2001.000','13264SC2008.000')nameListString='('+nameListString+')中的TestedWarPass.vendor_lot_id处“呃?天哪,这奏效了……我真是太感谢你了。”。我整天都在做这个查询。
String nameListString=nameList.collect{“$it'”}.join(“,”)
然后
String stmt=“select*from action\u group\u i18n where name in(${Sql.expand(nameListString)})”
但是要小心,因为这可能会让你面临Sql注入