简体   繁体   中英

Oracle dynamic cursor parameter

I have the following function :

function Rpt01 (param IN varchar2)
is 
vtype varchar2(50);

cursor Ccur is 
select * 
from table1, table2
where table1.date=table2.date
and table1.id=table2.id
and table1.cclient in (vtype);

begin

select (case when param='A' then 'A'
         when param='B' then 'B'
         else 'All' end)
into   vtype 
from   dual;

for rec in Ccur
loop
 do sth....
end loop; 
end;

My problem is that I want to assign multiple value to vtype in else case(A and B) instead of 'All'. I tried these ones 'A'||','||'B' or '''A'''||','||'''B'''. But that doesn't work. Can anyone suggest a solution ?

You can use SQL-defined collection type to pass list of values:

SQL> create table t
  2  as
  3  select 'A' x from dual union all
  4  select 'B' x from dual union all
  5  select 'C' x from dual union all
  6  select 'D' x from dual
  7  /

SQL> create type tab_varchar2 is table of varchar2(10)
  2  /

SQL> create or replace procedure get_rows
  2  (
  3   p_list in tab_varchar2
  4  )
  5  is
  6   cursor cur is
  7   select x from t where x in (select column_value from table(p_list));
  8  begin
  9    for c in cur loop
 10      dbms_output.put_line(c.x);
 11    end loop;
 12  end;
 13  /

SQL> set serveroutput on
SQL> begin
  2    get_rows(tab_varchar2('A','C'));
  3  end;
  4  /
A                                                                               
C                                                                               

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM