Oracle存储过程递归实现多对多关联

--单和卡是多对多的关系 ,进行关联
create table TEMP_COMPARE_GP
(
  AUTO_ID      VARCHAR2(50),
  REL_NO       VARCHAR2(20),   --作业单号码
  NO_COMPLETE  VARCHAR2(4) default 0,   --作业单是否查找完成
  CAR_NO       VARCHAR2(20),    --卡号码
  CAR_COMPLETE VARCHAR2(4) default 0    --卡是否查找完成
)
--数据如下:
A1    K1  0   0
A8    K8  0   0
A1    K2  0   0
A2    K1  0   0
A3    K1  0   0
A4    K2  0   0
A5    K2  0   0
A5    K6  0   0
A5    K7  0   0
A9    K9  0   0
--查询结果
A1    K1  0   0
A1    K2  0   0
A2    K1  0   0
A3    K1  0   0
A4    K2  0   0
A5    K2  0   0
A5    K6  0   0
A5    K7  0   0

--通过递归查询A1对应的所有单子
--涉及到的存储过程 Get_Weight_Caution_New1_EXT,Get_Weight_Caution_ForCAR,Get_Weight_Caution_ForBILL
create or replace procedure Get_Weight_Caution_ForCAR(GATEJOB_NO1 in varchar2)
   is
   /*******************************************************************************************

  VER        DATE        AUTHOR
  ---------  ----------  ---------------
  1.0        2009-4-20   jack.jia
  根据单子来找卡
  并且把卡考对应的  NO_COMPLETE =1
*******************************************************************************************/
   begin
      UPDATE TEMP_COMPARE_GP SET No_Complete='1'
     WHERE rel_no=GATEJOB_NO1 and  No_Complete='0';    
     commit;
   end Get_Weight_Caution_ForCAR;

create or replace procedure Get_Weight_Caution_ForBILL(CAR_NO1 in varchar2)
   is
  /*******************************************************************************************
 
  VER        DATE        AUTHOR
  ---------  ----------  ---------------
  1.0        2009-4-20   jack.jia
  根据卡来找单子
  并且把卡考对应的 CAR_COMPLETE =1
*******************************************************************************************/
   begin
            UPDATE TEMP_COMPARE_GP SET Car_Complete='1'
            WHERE car_no=CAR_NO1 and  Car_Complete='0';
            commit;
   end Get_Weight_Caution_ForBILL;

 

 

 

 

--主存储过程
create or replace procedure Get_Weight_Caution_New1_EXT(GATEJOB_NO1 in varchar2) is

 

cursor cur_stock is
    select TCG.Rel_No,TCG.Car_No,TCG.No_Complete,TCG.Car_Complete
         from  TEMP_COMPARE_GP TCG
         where rel_no=GATEJOB_NO1 and TCG.No_Complete='0';        
    CurRow cur_stock%rowtype;
   

   
   cursor cur_stock1 is
    select TCG.Rel_No,TCG.Car_No,TCG.No_Complete,TCG.Car_Complete
         from  TEMP_COMPARE_GP TCG
         where  TCG.No_Complete='0' AND Car_Complete='1';        
    CurRow1 cur_stock1%rowtype;
   
begin
 
 
  open Cur_Stock;
    loop
     fetch Cur_Stock
      into CurRow;
      exit when Cur_Stock%notfound;
       --循环调用卡找单子存储过程
       if (CurRow.car_complete='0') then             
           Get_Weight_Caution_ForBILL(CurRow.car_no);
       end if;      
    end loop;
    close Cur_Stock;
 
 
  --单子找卡
  Get_Weight_Caution_ForCAR(GATEJOB_NO1);
  --卡找单子
 
     
         open Cur_Stock1;
         loop
          fetch Cur_Stock1
          into CurRow1;
           exit when Cur_Stock1%notfound;
           Get_Weight_Caution_New1_EXT(CurRow1.Rel_No);
         end loop;
         close Cur_Stock1;
        
    commit;
   
   
     delete from TEMP_COMPARE_GP T where T.NO_COMPLETE='0' and T.CAR_COMPLETE='0';
    
  commit;
 
   
 
 end Get_Weight_Caution_New1_EXT;

posted @ 2009-04-23 09:43  jhtchina  阅读(3801)  评论(0)    收藏  举报