How do you specify IN clause in a dynamic query using a variable?

前端 未结 4 677
北恋
北恋 2020-12-06 08:32

In PL/SQL, you can specify the values for the IN operator using concatenation:

v_sql := \'select field1
from table1
where field2 in (\' || v_list || \')\';
<         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-06 09:15

    Unfortunately you cannot bind a list like this, however you can use a table function. Read this

    Here's an example of usage based on your code:

    declare
    
    cursor c_get_csv_as_tables is
    select in_list(food_list) food_list
    from emp_food
    where emp_type = 'PERM';
    
    cursor c_get_food_list (v_food_table varchar2Table)is
    select column_value food
    from TABLE(v_food_table);
    
    begin
        for i in c_get_csv_as_tables loop
            for j in c_get_food_list(i.food_list) loop
                dbms_output.put_line(j.food);
            end loop;
        end loop;
    end;
    

    I used here a column_value pseudocolumn

提交回复
热议问题