1

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

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.

任何想法 ?

4

1 回答 1

2

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 演示

于 2013-05-09T09:39:06.200 回答