Oracle存储过程循环函数使用实例:For Loop


create or replace procedure myOracleProc     is

   packqtyid varchar(16) :='';
begin
  
 ---1
 for temp_record in   
   (select  '4751456' as CDOTYPEID,   '规则1800' as description, 'aa'||to_char(sysdate,'yyMMddHH24miss')||rownum as  packingqtyid ,
 'aa'||to_char(sysdate,'yyMMddHH24miss')||rownum as packingqtyname,'4880838000000002' as  packingtypeid, p.productid ,'1800' as qtyperpack
from  Product p  
inner join ProductBase pb on pb.productbaseid = p.productbaseid
where    not       exists(select 1 
from   a_packingqty ap   where  p.productid = ap.productid and ap.description='规则1800')
 and pb.productname like '14%'
 and p.description like '太阳能%')
  
 loop 
    csiprdgetnextinstanceid('4751456',packqtyid);
   
   insert into a_packingqty 
   ( CDOTYPEID, description, packingqtyid,packingqtyname, packingtypeid, productid, qtyperpack )
   values(temp_record.cdotypeid,temp_record.description,packqtyid,temp_record.packingqtyname
   ,temp_record.packingtypeid,temp_record.productid,temp_record.qtyperpack );
   
  end loop ; 
 
 ---2
  for temp_record in  
    (
select  '4751456' as CDOTYPEID,  '规则36' as description ,'ab'||to_char(sysdate,'yyMMddHH24miss')||rownum as packingqtyid
,'ab'||to_char(sysdate,'yyMMddHH24miss')||rownum as packingqtyname,'488083800000001d' as  packingtypeid, p.productid ,'36' as qtyperpack
from  Product p  
inner join ProductBase pb on pb.productbaseid = p.productbaseid
where    not       exists(select 1 
from   a_packingqty ap   where  p.productid = ap.productid and ap.description='规则36')
 and pb.productname like '14%'
 and p.description like '太阳能%' )
  loop 
    csiprdgetnextinstanceid('4751456',packqtyid);
   
   insert into a_packingqty 
   ( CDOTYPEID, description, packingqtyid,packingqtyname, packingtypeid, productid, qtyperpack )
   values(temp_record.cdotypeid,temp_record.description,packqtyid,temp_record.packingqtyname
   ,temp_record.packingtypeid,temp_record.productid,temp_record.qtyperpack );
   
  end loop ; 
 
 
 
 
  
 commit;
 
end  myOracleProc

你可能感兴趣的:(oracle,数据库)