Reputation: 155
Let's say that Customer 4 wishes to increase their order from 23 to 100. Enable the user to type in:
I want to Write a PL/SQL function to receive these two values and update the sales table to reflect this change. Print out on screen from the main section of code the total quantity for customer 4 before and after the update. Please help I have tried with the following code but not sure about the structure
CREATE OR REPLACE TRIGGER orders_before_insert
BEFORE INSERT
ON ord
FOR EACH ROW
DECLARE
v_price NUMBER;
new number;
BEGIN
SELECT pr INTO v_price FROM product where product_id =:new.product_id
;
-- Update create_date field to current system date
:new.total_cost := :new.quantity * v_price;
END;
Upvotes: 0
Views: 156
Reputation: 146219
Here is a proof of concept procedure. It doesn't have the validation or error handling we would expect in a proper procedure.
create or replace procedure update_order
(p_order_id in orders.id%type
, p_additional_qty in orders.qty%type
, p_orig_total out number
, p_new_total out number )
is
l_total number;
l_orig number;
begin
update orders
set qty = qty + p_additional_qty
where id = p_order_id
returning (qty - p_additional_qty)* price
, qty * price into l_orig, l_total;
p_orig_total := l_orig;
p_new_total := l_total;
end update_order;
/
In SQL*Plus we use the ACCEPT command to get input values from a user. We declare variables to hold computed values with VAR, and output them with PRINT.
Here is the test data:
SQL> select * from orders;
ID QTY PRICE
---------- ---------- ----------
42 23 19.99
SQL>
And here is how we call the procedure using the SQL*Plus EXECUTE command:
SQL> var tot number
SQL> var orig number
SQL> accept order_id prompt "enter order ID: "
enter order ID: 42
SQL> accept add_qty prompt "please enter add qty: "
please enter add qty: 77
SQL> exec update_order (&order_id, &add_qty, :orig, :tot)
PL/SQL procedure successfully completed.
SQL> print :orig
ORIG
----------
459.77
SQL> print :tot
TOT
----------
1999
SQL>
To script it, just place all the commands in a text file, and run it in SQL*Plus like this:
SQL> @your_script.sql
Note that I have deviated from your assignment's instructions in a couple of ways.
Upvotes: 3