【问题标题】:oracle sql stored procedure that creates temporary local cache of number(10)oracle sql存储过程,创建数字(10)的临时本地缓存
【发布时间】:2021-02-11 11:30:37
【问题描述】:

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

仅供参考,对于选择语句,我在循环中使用游标,这就是我希望更新本地临时缓存/表的方式。

提前感谢您的帮助。

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

【问题讨论】:

  • 您使用的是哪个版本的 Oracle?

标签: sql oracle stored-procedures plsql


【解决方案1】:

您不需要 GTT 或集合来假脱机密钥。此外,甚至不需要 plsql;你可以在一个单一的声明中做到这一点。由于您没有发布 where 子句,我将保持相同的基本选择结构:

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

这可能可以进一步简化为 where 子句中与 OR 连接的单个子选择。

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); 

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

【讨论】:

  • 我想做的不仅仅是用这些存储的数字更新一个字段,我还想做一些插入等。所以我更愿意遍历存储的缓存以进行适当的插入、更新等
  • 然后,在将来,将该类型信息放入您对所尝试内容的描述中。当我们只有部分需求时,很难给出一个好的答案。很高兴你得到了答案。
【解决方案2】:

如果您不知道自己可能拥有多少键,全局临时表 (GTT) 是一种非常好的方法,因为 GTT 将假脱机到临时存储而不是耗尽您的会话内存。

但如果你绝对确定尺寸很小,那么嵌套表就可以做到,例如

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.

【讨论】:

  • 如何将数字添加到emplist并从emplist中选择一个值?你能像表格一样使用插入或选择语句吗?
  • 在文档docs.oracle.com/en/database/oracle/oracle-database/19/adobj/… 中对 PLSQL 中的对象类型进行了完整的处理,但正如其他人所说,请确保您确实需要走这条路
猜你喜欢
  • 2010-11-14
  • 2017-12-21
  • 1970-01-01
  • 2013-11-01
  • 1970-01-01
  • 2011-10-29
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
相关资源
最近更新 更多