最近在写一个存档的procedure时,因为条件不同,组成的SQL也不同。
1 CREATE OR REPLACE 2 PROCEDURE led_bl_issue_save_new ----大标签发料存档LED031C 3 ( 4 i_worknosid NUMBER,---工单ID 5 i_led_partno VARCHAR2,---LED料号 6 i_led_partid VARCHAR2,---LED料号ID 7 i_bincode1 VARCHAR2, ---BIN1 8 i_datecode1 VARCHAR2,---周期1 9 i_bin1_qty number,---BIN1数量 10 i_bincode2 VARCHAR2,--BIN2 11 i_datecode2 VARCHAR2,---周期2 12 i_bin2_Qty number,---BIN2数量 13 i_iusqty NUMBER,---发料数量 14 i_iustype NUMBER,---发料类型 15 i_bl1_sid NUMBER,---大标签1ID 16 i_bl2_sid NUMBER,---大标签2ID 17 i_pmp_bin VARCHAR2,---喷墨BIN 18 i_usersid NUMBER----USERID 19 ) 20 IS 21 xMse0306_f001 mse0306.f001%TYPE := 0; 22 xReelno sajet.g_part_map.part_sn%TYPE := ‘N/A‘; 23 type ref_cursor is ref cursor; 24 xCursor ref_cursor ; 25 xSqlCommand varchar(2000); 26 xBin1Qty_Sys number :=0;-----系统中的BIN1数量 27 xBin1Qty_New number := 0;-----待更新的BIN1数量 28 xBin2Qty_Sys number :=0;-----系统中的BIN2数量 29 xBin2Qty_New number := 0;-----待更新的BIN2数量 30 xBinQty_Sys number :=0;-----系统中的BIN数量 31 xBinQty_New number := 0;-----待更新的BIN数量 32 BEGIN 33 34 ----获得MSE0306.F001 35 IF i_iustype=0 THEN 36 xSqlCommand:= ‘SELECT f001 FROM MSE0306 WHERE WK_ORD_ID=‘ || i_worknosid || ‘ AND TYPE_ID=0 AND STATUS=0‘|| 37 ‘ AND BIN2 IS NULL AND DATECODE2 IS NULL AND BIN1=‘‘‘|| i_bincode1 ||‘‘‘‘; 38 if i_datecode1 =‘‘ then 39 xSqlCommand := xSqlCommand ||‘ AND DATECODE1 is null‘; 40 else 41 xSqlCommand := xSqlCommand ||‘ AND DATECODE1 = ‘‘‘|| i_datecode1 ||‘‘‘‘; 42 end if; 43 ELSE 44 xSqlCommand := ‘SELECT f001 FROM MSE0306 WHERE WK_ORD_ID=‘|| i_worknosid ||‘ AND TYPE_ID=1 AND STATUS=0‘ || 45 ‘ AND BIN1=‘‘‘|| i_bincode1 ||‘‘‘‘ ||‘ AND BIN2=‘‘‘|| i_bincode2 ||‘‘‘‘ ; 46 47 If i_datecode1 =‘‘ Then 48 xSqlCommand := xSqlCommand || ‘ AND DATECODE1 is null‘; 49 Else 50 xSqlCommand := xSqlCommand || ‘ AND DATECODE1=‘‘‘|| i_datecode1 ||‘‘‘‘; 51 End If; 52 53 If i_datecode2=‘‘ Then 54 xSqlCommand := xSqlCommand ||‘ AND DATECODE2 is null‘ ; 55 Else 56 xSqlCommand := xSqlCommand ||‘ AND DATECODE2=‘‘‘|| i_datecode2 ||‘‘‘‘; 57 End If; 58 END IF; 59 60 open xCursor for xSqlCommand; 61 LOOP 62 FETCH xCursor INTO xmse0306_f001; 63 EXIT WHEN xCursor %notfound ; 64 END LOOP; 65 close xCursor; 66 67 if xMse0306_f001>0 then 68 select nvl(qty1,0),nvl(qty2,0),nvl(qty,0) 69 into xBin1Qty_Sys,xBin2Qty_sys,xBinqty_sys 70 from mse0306 71 where f001 = xMse0306_f001; 72 end if; 73 xBin1Qty_New := xBin1Qty_Sys + i_bin1_qty; 74 xBin2Qty_New := xBin2Qty_Sys + i_bin2_qty; 75 xBinQty_New := xBinqty_sys + i_bin1_qty + i_bin2_qty; 76 77 ---处理MSE0306 78 if xMse0306_f001 = 0 then 79 INSERT INTO mse0306(wk_ord_id, bin1, datecode1,qty1, bin2, datecode2, qty2, qty,type_id, creat_time, creat_userid, item_partno,runsheet) 80 VALUES (i_worknosid, i_bincode1,i_datecode1, xBin1Qty_New ,i_bincode2, i_datecode2 , xBin2Qty_New , xBinQty_New, i_iustype,sysdate, i_usersid , i_led_partno, i_pmp_bin ); 81 82 open xCursor for xSqlCommand; 83 LOOP 84 FETCH xCursor INTO xmse0306_f001; 85 EXIT WHEN xCursor %notfound ; 86 END LOOP; 87 close xCursor; 88 89 else 90 UPDATE mse0306 91 SET qty1=xBin1Qty_New, 92 qty2=xBin1Qty_New, 93 qty = xBinQty_New , 94 up_time = SYSDATE, 95 up_userid = i_usersid 96 WHERE f001 = xMse0306_f001; 97 end if; 98 99 ----更新大標籤 100 UPDATE mse0304 101 SET f007 = ‘N‘, 102 f013 = xMse0306_f001, 103 f014 =i_usersid, 104 f015 = SYSDATE 105 WHERE f001 = i_bl1_sid; 106 107 IF i_iustype = 1 108 THEN 109 UPDATE mse0304 110 SET f007 = ‘N‘, 111 f013 = xMse0306_f001, 112 f014 = i_usersid, 113 f015 = SYSDATE 114 WHERE f001 = i_bl2_sid; 115 END IF; 116 117 ----更新小标签,写 发料表 118 FOR l_cursor IN (SELECT m305.f003 119 FROM mse0304 m304, mse0305 m305 120 WHERE m304.f001 = m305.f002 AND m304.f001 = i_bl1_sid) 121 LOOP 122 xreelno := l_cursor.f003; 123 124 INSERT INTO sajet.g_part_led_issue 125 (wk_ord_id, part_id, part_sn, upd_time, upd_uid 126 ) 127 VALUES (i_worknosid, i_led_partid , xreelno, SYSDATE, i_usersid 128 ); 129 130 UPDATE mse0305 131 SET f005 = ‘N‘ 132 WHERE f003 = xreelno; 133 END LOOP; 134 135 IF i_iustype = 1 136 THEN 137 FOR l_cursor IN (SELECT m305.f003 138 FROM mse0304 m304, mse0305 m305 139 WHERE m304.f001 = m305.f002 AND m304.f001 = i_bl2_sid) 140 LOOP 141 xreelno := l_cursor.f003; 142 143 INSERT INTO sajet.g_part_led_issue 144 (wk_ord_id, part_id, part_sn, upd_time, upd_uid 145 ) 146 VALUES (i_worknosid, i_led_partid, xreelno, SYSDATE, i_usersid 147 ); 148 149 UPDATE mse0305 150 SET f005 = ‘N‘ 151 WHERE f003 = xreelno; 152 END LOOP; 153 END IF; 154 155 ---- DBMS_OUTPUT.PUT_LINE(‘存檔OK...‘); 156 /* 157 EXCEPTION 158 WHEN Others THEN 159 ----DBMS_OUTPUT.PUT_LINE(SQLCODE||‘---‘||SQLERRM); 160 rollback;*/ 161 END; 162 /
时间: 2024-10-28 23:42:15