一、
加工处理后要变成如下效果
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; /