oracle sql 创建 number(10) 的临时本地缓存的存储过程

oracle sql stored procedure that creates temporary local cache of number(10)

我需要在存储过程中执行多个 select 语句,然后在过程结束时根据 select 语句中检索到的键更新表。在存储过程中执行此操作的最佳方法是什么,我查看了全局临时表,但似乎不是最佳方法。

仅供参考,对于 select 语句,我在循环中使用游标,这就是我希望更新本地温度的方式 cache/table。

提前感谢您的帮助。

create or replace procedure...
begin

select primary_keys from table..
-- save these keys to a temp cache or table

select primary_keys from table
-- save these additional keys to a temp cache or table

update table set field = 1 where primary_key in (select keys in temp cache or table)

commit;
delete temp cache or table

全局临时 table (GTT) 如果您不知道您可能拥有多少个密钥,这是一个非常好的方法,因为 GTT 将假脱机到临时存储而不是炸毁您的会话内存.

但是如果你绝对确定尺寸很小,那么一个嵌套table就可以了,eg

SQL> set serverout on
SQL> declare
  2    emplist sys.odcinumberlist;
  3    l_count int;
  4  begin
  5    select empno
  6    bulk collect into emplist
  7    from scott.emp;
  8
  9    for i in 1 .. emplist.count
 10    loop
 11      dbms_output.put_line(emplist(i));
 12    end loop;
 13
 14    select count(*)
 15    into   l_count
 16    from   scott.emp
 17    where  sal > 2000
 18    and  empno in ( select column_value from table(emplist));
 19
 20    dbms_output.put_line('l_count='||l_count);
 21
 22  end;
 23  /
7369
7499
7521
7566
7654
7698
7782
7788
7839
7844
7876
7900
7902
7934
l_count=6

PL/SQL procedure successfully completed.

您不需要 GTT 或集合来假脱机密钥。此外,甚至不需要 plsql;您可以在一条语句中完成。由于您没有 post where 子句,我将保持相同的基本 select 结构:

update table 
   set field = 1 
 where primary_key in  
       ( select primary_keys from table    -- first select    
         union 
         select primary_keys from table    -- second select
       );

这可能可以进一步简化为与 OR 相关的 where 子句中的单个子 select。

update table 
   set field = 1 
 where primary_key in  
       ( select primary_keys from table      
          where (clause form first select)   
             or (clause form second select) 
       );  

根据这些条款可能减少到:

update table 
   set field = 1 
 where (clause form first select)   
    or (clause form second select); 

但我需要这些条款来做出决定。