Oracle11g Oracle:存储过程中大容量收集游标的动态表名

Oracle11g Oracle:存储过程中大容量收集游标的动态表名,oracle11g,Oracle11g,我正在开发一个带有批量收集游标的存储过程。 我最初开发了一个带有批量收集的静态游标,如下所示: procedure p_get_x ( p_x in NUMBER ) is l_var1 is number; cursor c_x is select col1, col2 from tbl1 where col1 = l_var1 ; t_x c_x%rowtype;

我正在开发一个带有批量收集游标的存储过程。 我最初开发了一个带有批量收集的静态游标,如下所示:

procedure p_get_x ( p_x  in NUMBER )
    is
      l_var1 is number;
      cursor c_x is
      select 
        col1, col2
        from tbl1
      where col1 = l_var1
      ;
      t_x c_x%rowtype; 
      TYPE tab_x IS TABLE OF t_x%TYPE;
      tb_x tab_x; 

    BEGIN      
             open c_x;
             fetch c_x bulk collect into tb_x ;
             close c_x
           ;
          for idx in 1 .. tb_x.count 
            loop
              insert into tbl2
               (
                col1,
                col2
               )
               values (
               tb_x(idx).col1,
               tb_x(idx).col2
               );
          end loop;
          commit;
      end if;    
end p_get_x;
要求是创建一个通用程序。 基于输入p_x,过程必须执行不同的游标定义,但一次执行中只有一个游标。 游标表名称将不同,每个表将有一些通用列名和一些特定列名。 示例:-表a-col1、col2、col3 表b-col1、col3、col4

如何创建动态游标wilt批量收集,因为该表中平均有500万行

我试过的参考资料:


谢谢

如果真的只有两条路径,那么使用动态SQL可能没有意义。使用静态SQL,只需向代码中添加分支逻辑即可。差不多

create or replace procedure do_something( p_table_name in varchar2 )
as
  type t1_tbl is table of t1%rowtype;
  type t2_tbl is table of t2%rowtype;

  l_t1s t1_tbl;
  l_t2s t2_tbl;
begin
  if( p_table_name = 'T1' )
  then
    select *
      bulk collect into l_t1s
      from t1;

    forall i in 1 .. l_t1s.count
      insert into dest_table( columns )
        values( l_t1s(i).col1, ... l_t1s(i).colN );
  elsif( p_table_name = 'T2' )
  then
    select *
      bulk collect into l_t2s
      from t2;

    forall i in 1 .. l_t2s.count
      insert into dest_table( columns )
        values( l_t2s(i).colA, ... l_t2s(i).colN );
  end if;
end;

真的只有两种选择吗?如果是这样的话,使用一个简单的
If
语句(使用静态SQL分支到两条路径中的一条)比尝试使用动态SQL要好得多。If语句在declare语句中不起作用有可能将ref cursor和bulk绑定在一起吗?我使用的是Oracle 18我建议您声明两个不同的游标(假设您要使用显式游标),并在过程主体中使用
IF
语句,根据输入处理其中一个游标。您能提供参考吗