【问题标题】:Dynamic query, parameters not working for where in动态查询,参数不适用于 where in
【发布时间】:2013-05-03 14:31:52
【问题描述】:

我正在使用 oracle 上的动态查询,当我将字符串参数传递给 where in 条件时,它不起作用。

for x in (
            SELECT DISTINCT column_id
            FROM table
            WHERE column_id in (in_column_ids)          
            /* WHERE column_id in (15,16,17) =>works */
            /* => in_column_ids is a varchar type which 
                  holds comma separated value */
            and column_title=in_column_title /* works */
    )

在这里,如果我将值直接保留在该 in_column_ids 上,则查询有效。 但是,作为参数传递的值似乎不适用于where in。

有什么想法吗?

【问题讨论】:

    标签: sql database oracle oracle10g where-in


    【解决方案1】:

    IMO,您必须使用 regexp_substr 拆分逗号分隔的变量。您的查询应该是这样的:

    for x in (
                SELECT DISTINCT column_id
                FROM table
                WHERE column_id in (
                SELECT DISTINCT regexp_substr(in_column_ids,'[^,]+', 1, LEVEL) FROM DUAL
                CONNECT BY regexp_substr(in_column_ids, '[^,]+', 1, LEVEL) IS NOT NULL
                )          
                /* WHERE column_id in (15,16,17) =>works */
                /* => in_column_ids is a varchar type which 
                      holds comma separated value */
                and column_title=in_column_title /* works */
        )
    

    查看SQLFIDDLE DEMO

    【讨论】:

      猜你喜欢
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 2014-04-18
      • 2015-09-28
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 2021-10-14
      相关资源
      最近更新 更多