Quote:
Originally posted by appoooh
The procedure compiles now, so I'm making some progress, but when I call it with EXEC update_price('30-AUG-03'), price doesn't actually get updated. Could someone tell me why?
CREATE OR REPLACE PROCEDURE update_price
(p_cutoff IN CHAR)
IS
CURSOR item_cur IS
SELECT price, start_date
FROM items, on_sale
WHERE items.id = on_sale.item_id
AND p_cutoff = on_sale.start_date
FOR UPDATE of price;
BEGIN
FOR item_rec IN item_cur LOOP
UPDATE items
SET price = price * 0.9
WHERE CURRENT OF item_cur;
DBMS_OUTPUT.PUT_LINE(TO_CHAR(item_rec.price, '$99.99') || ' ' || TO_CHAR(item_rec.start_date));
END LOOP;
COMMIT;
END;
/
EXEC update_price('30-AUG-03');
|
Seems OK to me:
Code:
SQL> select * from items;
ID NAME PRICE
---------- ---------- ----------
1 thing 10
SQL> select * from on_sale;
ITEM_ID START_DATE END_DATE
---------- ----------- -----------
1 30-AUG-2003 31-DEC-2003
SQL> EXEC update_price('30-AUG-03');
$10.00 30-AUG-2003
PL/SQL procedure successfully completed.
SQL> select * from items;
ID NAME PRICE
---------- ---------- ----------
1 thing 9
Are you being confused by the fact that the output from PUT_LINE still shows the old price?
BTW, it could more efficiently be written without a cursor like this:
Code:
CREATE OR REPLACE PROCEDURE update_price
(p_cutoff IN CHAR)
IS
BEGIN
update items
set price = price*0.9
where id in
( select item_id
from on_sale
where start_date = to_date(p_cutoff,'DD-MON-RR')
);
END;
/
(I would not perform the commit in the procedure - prefer to leave that decision to the caller).