首页 > 解决方案 > 带有游标的过程,以便使用视图增量填充表

问题描述

看法:

po     po_line month year amount          
41216    10    jan   2018  3000           
41216    20    feb   2018  4000           
41216    30    Aug   2018  6000
54321    10    march 2018  7000
32133    10    feb   2018  5000

桌子:

po     po_line month year amount
41216    10    jan   2018  3000 
41216    20    feb   2018  4000

我需要一个带有游标的过程,以便使用视图增量填充表。

这个想法是,当视图被更新时,更新的数据应该被插入到表中。

代码:

create or replace procedure prc as 
  cursor c1 is 
    select * 
    from vw_po_tab 
    where po||po_line not in(select po||po_line from po_tab1);
begin
  for i in c1 loop
    insert into po_tab1(po,po_line,month,year, amount)
    values(i.po,i.po_line,i.month,i.year, i.amount);
   end loop;
end;

标签: oracleplsqloracle11g

解决方案


您不需要循环(甚至不需要过程)。您可以使用一个 INSERT 语句来做到这一点:

insert into po_tab1(po,po_line,month,year, amount)
select po, po_line, month, year, amount
from vw_po_tab 
where (po, po_line) not in (select po, po_line  
                            from po_tab1);

对于多列IN条件,您不应连接两个值,而应使用两个单独的列。因为如果你将它们连接起来1,12,并且11,2将是相同的。


推荐阅读