【问题标题】:Oracle dynamic cursor parameterOracle 动态游标参数
【发布时间】:2014-02-13 14:32:34
【问题描述】:

我有以下功能:

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'''。但这不起作用。任何人都可以提出解决方案吗?

【问题讨论】:

    标签: oracle dynamic cursor


    【解决方案1】:

    您可以使用 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                                                                               
    

    【讨论】:

    • 最好使用x member of p_list而不是in。更清晰、美观、快速
    • 德米特里,表 t 和类型 tab_varchar2 是永久的还是临时的?
    猜你喜欢
    • 2014-11-20
    • 1970-01-01
    • 2012-08-08
    • 2021-01-14
    • 1970-01-01
    • 1970-01-01
    • 2017-03-17
    • 2011-10-12
    • 1970-01-01
    相关资源
    最近更新 更多