Oracle动态游标参数

时间:2014-02-13 06:04:50

标签: oracle dynamic cursor

我有以下功能:

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;

我的问题是我想在其他情况下(A和B)而不是“全部”为 vtype 分配多个值。 我试过这些'A'||','||'B'或'''A'''||','||''''B'''。但这不起作用。有人可以提出解决方案吗?

1 个答案:

答案 0 :(得分:2)

您可以使用SQL定义的集合类型来传递值列表:

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