动态查询,参数不适用于哪里

时间:2013-05-09 07:06:27

标签: sql database oracle oracle10g where-in

我正在使用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

有什么想法吗?

1 个答案:

答案 0 :(得分: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 DEMO