带游标的存储过程例子,很经典

来源:互联网 发布:海外婚礼 知乎 编辑:程序博客网 时间:2024/06/14 23:20
  1. create or replace procedure sum_storage    
  2. is  
  3.     plant  g_containerinv.plant%type;   
  4.     sloc   g_containerinv.sloc%type;   
  5.     part   g_containerinv.partno%type;   
  6.     qty    g_containerinv.qty%type;   
  7.     
  8.   cursor c_sumqty   
  9.     is  
  10.   select plantid,whid,partno,sum(qtyperuom) as totalqty from g_container group by plantid,whid,partno;   
  11.       
  12.  vr_intoqty   c_sumqty%rowtype;               
  13.   
  14. begin  
  15.    open c_sumqty;   
  16.     loop   
  17.      fetch c_sumqty   into vr_intoqty;   
  18.   
  19.       exit when c_sumqty%notfound;   
  20.       
  21.         plant:  = vr_intoqty.plantid;   
  22.          sloc:  = vr_intoqty.whid;   
  23.          part:  = vr_intoqty.partno;   
  24.           qty:  = vr_intoqty.totalqty;   
  25.       
  26.     insert into g_containerinv(timekey, plant, sloc, partno, qty, editdate, operater)    
  27.     values (to_char(sysdate,'yyyymmddhh24miss'),plant,sloc,part,qty,sysdate,'wms');   
  28.     
  29.     end loop;   
  30.     
  31.    close c_sumqty;   
  32.  commit;   
  33.      
  34. end sum_storage;   
  35. /

例子2

  1. CREATE OR REPLACE PROCEDURE add_sup_temp IS  
  2.   v_group_id  sup_temp.group_id % TYPE;   
  3.   v_item_id   sup_temp.item_id % TYPE;   
  4.   v_item_name sup_temp.item_name % TYPE;   
  5.   v_cnt_group NUMBER(4);   
  6.   
  7.   CURSOR c_product_group IS  
  8.     SELECT group_id, item_id, lang, item_name, classify_id, parent_item   
  9.       FROM viewA;   
  10.   
  11.   CURSOR c_groupCursor(p_group_id sup_acl_product_group.group_id % TYPE) IS  
  12.     SELECT group_id FROM tableB;   
  13.   
  14. BEGIN  
  15.   DELETE FROM sup_acl_product_group;   
  16.   OPEN c_product_group;   
  17.   LOOP   
  18.     FETCH c_product_group   
  19.       INTO v_group_id, v_item_id, v_item_name;   
  20.     EXIT WHEN c_product_group%NOTFOUND;   
  21.      
  22.     OPEN c_groupCursor(v_group_id);   
  23.     LOOP   
  24.       FETCH c_groupCursor   
  25.         INTO v_c_group_id;   
  26.       EXIT WHEN c_groupCursor%NOTFOUND;   
  27.        
  28.       SELECT COUNT(group_id)   
  29.         INTO v_cnt_group   
  30.         FROM sup_acl_product_group pg   
  31.        WHERE pg.group_id = v_c_group_id   
  32.          AND pg.item_id = v_item_id;   
  33.       IF v_cnt_group <> 0 THEN  
  34.         UPDATE sup_acl_product_group pg SET pg.last_update_time = sysdate;   
  35.       ELSE  
  36.         INSERT INTO sup_acl_product_group   
  37.           (group_id, item_id, item_name, last_update_time)   
  38.         VALUES  
  39.           (v_c_group_id, v_item_id, v_item_name, sysdate);   
  40.       END IF;   
  41.     END LOOP;   
  42.     CLOSE c_groupCursor;   
  43.   END LOOP;   
  44.   CLOSE c_product_group;   
  45.   COMMIT;   
  46.   
  47. END;

例子3

 

  1. create or replace procedure p_tmp   
  2. begin  
  3.   declare  
  4.     cursor c_cur is  
  5.            select  st.business_id            as business_id,   
  6.                       st.userid       as userid,   
  7.                       st.end_date        as end_date,   
  8.                       st.creation_date  as creation_date   
  9.         from tableH st   
  10.        where 1=1;   
  11.             
  12.     v_row                c_cur%rowtype;   
  13.     v_business_id_t      varchar2(200);   
  14.   begin  
  15.     open c_cur;   
  16.     loop   
  17.       fetch c_cur   
  18.         into v_row;   
  19.        
  20.       exit when c_cur%notfound;   
  21.        
  22.       select count(n.business_id)   
  23.         into v_business_id_t   
  24.         from tableM n   
  25.        where n.business_id = v_row.business_id   
  26.          and n.userid = v_row.userid;   
  27.        
  28.       if v_business_id_t = 0 then  
  29.         insert into tableN   
  30.           (business_id, userid, end_date, creation_date)   
  31.         values  
  32.           (v_row.business_id,   
  33.            v_row.userid,   
  34.            v_row.end_date,   
  35.            v_row.creation_date);   
  36.          
  37.       end if;   
  38.        
  39.       if v_business_id_t > 0 then  
  40.         update tableN t   
  41.            set t.end_date = v_row.end_date   
  42.          where t.business_id = v_row.business_id   
  43.            and t.userid = v_row.userid;   
  44.       end if;   
  45.     end loop;   
  46.      
  47.     close c_cur;   
  48.     commit;   
  49.   end;   
  50. exception   
  51.   when others   
  52.      
  53.    then  
  54.     rollback;   
  55.     dbms_output.put_line('异常啦');   
  56. end;

 

 

 

 

 

原创粉丝点击