Description: This code accumulates 100 units of part number 9999 from various storage bins
Comment: (none)
Language: PL/SQL Highlight Mode: PLSQL Last Modified: March 03rd, 2009
/*
** This code accumulates 100 units of part number 9999 from
** various storage bins.
*/DECLARECURSOR bin_cur(part_number NUMBER)ISSELECT amt_in_bin
FROM bins
WHERE part_num = part_number AND
amt_in_bin >0ORDERBY bin_num
FORUPDATEOF amt_in_bin;
bin_amt bins.amt_in_bin%TYPE;
total_so_far NUMBER(5):=0;
amount_needed CONSTANTNUMBER(5):=100;
bins_looked_at NUMBER(3):=0;BEGINOPEN bin_cur(9999);WHILE total_so_far < amount_needed LOOPFETCH bin_cur INTO bin_amt;EXITWHEN bin_cur%NOTFOUND;/* If we exit, there's not enough to *
* satisfy the order. */
bins_looked_at := bins_looked_at +1;IF total_so_far + bin_amt < amount_needed THENUPDATE bins SET amt_in_bin =0WHERECURRENTOF bin_cur;-- take everything in the bin
total_so_far := total_so_far + bin_amt;ELSE-- we finally have enoughUPDATE bins SET amt_in_bin = amt_in_bin
-(amount_needed - total_so_far)WHERECURRENTOF bin_cur;
total_so_far := amount_needed;ENDIF;ENDLOOP;CLOSE bin_cur;INSERTINTO temp VALUES(NULL, bins_looked_at,'<- bins looked at');COMMIT;END;/