Oracle:存储过程中批量收集游标的动态表名称

问题描述 投票:0回答:1

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

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,该过程必须执行不同的游标定义,但是在一个执行中只能有1个游标。游标表名称将有所不同,并且每个表将具有一些公用列名称和少量特定列名称。例如:-表a-col1,col2,col3表b-col1,col3,col4

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

我尝试过的参考资料:Dynamic Variable in Cursor in Oracle stored proceduredynamic table name in cursor

谢谢

oracle11g
1个回答
0
投票

如果确实只有两条路径,则诉诸动态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;
© www.soinside.com 2019 - 2024. All rights reserved.