• ORACLE 对一个表进行循环查数,再根据MO供给数量写入另一个新表


    一、

    加工处理后要变成如下效果

    create table test1 (sonum varchar2(10),lineid varchar2(10),qty int ,qty2 int ,remarks varchar2(10));
    create table test2(moid varchar2(10),qty int ,sonum varchar2(10),lineid varchar2(10),qty2 int,remarks varchar2(10))
    create table test3(sonum varchar2(10),lineid varchar2(10),moid varchar2(10),qty int )
    
    insert into test1 values('SO1','10','500','','')
    insert into test1 values('SO1','10','1000','','')
    insert into test1 values('SO1','10','1500','','')
    insert into test1 values('SO2','10','10','','')
    insert into test2 values('mo1','4000','SO1','10','4000','')
    insert into test2 values('mo1','200','SO2','10','2000','')
    CREATE OR REPLACE PROCEDURE test123 (EXITCODE OUT NUMBER)
    is
    
       V_so     VARCHAR2 (10); 
       V_lineid     VARCHAR2 (10); 
       V_mo     VARCHAR2 (10);  
    
       
    BEGIN
    
    
     FOR L_RECORD IN (select * from test1) LOOP
    
         --注意此处还需要增加判断是否有数据写入三个变量,不然会报错,此处略
         select  moid,sonum,lineid  
            INTO V_mo,  V_so, V_lineid 
         from  test2 where sonum=L_RECORD.sonum and lineid=L_RECORD.lineid and rownum=1;
        
        --写入数据到新表时,需考虑test2 MO表中的数量是否能否满足test1供给,此处略
        insert into test3 values (V_so,V_lineid,V_mo,L_RECORD.qty);
         
    
     END LOOP

    二、

    create table test1 (sonum varchar2(10),lineid varchar2(10),qty int ,qty2 int ,remarks varchar2(10),planid varchar2(10));
    create table test2(moid varchar2(10),qty int ,sonum varchar2(10),lineid varchar2(10),qty2 int,remarks varchar2(10))
    create table test3(sonum varchar2(10),lineid varchar2(10),moid varchar2(10),qty int )
    
    insert into test1 values('SO1','10','500','500','','1');
    insert into test1 values('SO1','10','1000','1000','','2');
    insert into test1 values('SO2','10','10','','10','1');
    insert into test2 values('mo1','400','SO1','10','400','');
    insert into test2 values('mo2','500','SO1','10','500','');
    insert into test2 values('mo3','600','SO1','10','600','');
    insert into test2 values('mo4','700','SO1','10','700','');
    insert into test2 values('mo5','200','SO2','10','200','');
    CREATE OR REPLACE PROCEDURE STG.test321 (EXITCODE OUT NUMBER)
    is
    
        PROCEDURE LoopAddData(TSONUM VARCHAR2,TLINEID VARCHAR2,TQTY INT ,TQTY2 INT,TPLANID VARCHAR2)
        as
               V_so     VARCHAR2 (10); 
                V_lineid     VARCHAR2 (10); 
                V_mo     VARCHAR2 (10);  
                V_qty    int;  
                V_qty2   int;  --供给数量
                V_qtymid int;  ---循环时需重新查询最新需求数量
        begin
            
            
            --注意此处还需要增加判断是否有数据写入三个变量,不然会报错
            SELECT  moid,sonum,lineid ,qty,qty2
                INTO V_mo,  V_so, V_lineid ,V_qty,V_qty2
             FROM  test2 where sonum=TSONUM and lineid=TLINEID and rownum=1;
             
             IF TQTY2<=V_qty2 -- 需求数量小于供给数量
             THEN
                INSERT INTO TEST3 values (V_so,V_lineid,V_mo,TQTY2);
                UPDATE TEST1 SET qty2=0 where SONUM=TSONUM  AND LINEID=TLINEID  AND PLANID=TPLANID;
                UPDATE TEST2 SET QTY2=QTY2-TQTY2  where SONUM=TSONUM  AND LINEID=TLINEID AND MOID=V_mo;
                commit;
                
             ELSE 
                --当余下的需求数量大于供给数量时,再次循环
                SELECT QTY2 INTO V_qtymid from test1 where SONUM=TSONUM AND LINEID=TLINEID AND PLANID=TPLANID;
                LoopAddData(TSONUM,TLINEID,TQTY,V_qtymid,TPLANID);
             END IF;
        end;
        
    
    
    BEGIN
    
     FOR L_RECORD IN (select SONUM,LINEID,QTY,QTY2,PLANID from test1) 
     
     LOOP
        
            LoopAddData(L_RECORD.SONUM,L_RECORD.LINEID,L_RECORD.QTY,L_RECORD.QTY2,L_RECORD.PLANID);
         --注意此处还需要增加判断是否有数据写入三个变量,不然会报错
         --select  moid,sonum,lineid  
          --  INTO V_mo,  V_so, V_lineid 
         --from  test2 where sonum=L_RECORD.sonum and lineid=L_RECORD.lineid and rownum=1;
        
        --insert into test3 values (V_so,V_lineid,V_mo,L_RECORD.qty);
         
    
     END LOOP;
    
    
    
    END;
    /
  • 相关阅读:
    java基础之条件运算符
    java基础之x++与++x
    java基础之三种注释及API帮助文档的生成
    java基础之类型转换及常量的应用
    java基础之数据类型
    java基础之标识符
    生活小插曲(长篇连载,持续更新ing)^_^
    记录那个刚毕业,还不太富裕的那个人的生活
    记录两次小组会议总结
    这是大娃笔记里的一首散文诗
  • 原文地址:https://www.cnblogs.com/Snowfun/p/7954288.html
Copyright © 2020-2023  润新知