简体   繁体   中英

PL/SQL Storing and calling procedure

I am currently working on a PL/SQL problem where I have to create a cursor inside a procedure that will give the appropriate discount for a given item (10% for prices >= $100 and 5% for prices >= $10). When I have to call the procedure, I need to display the Order Number, customer First Name, Last Name, and the Total Net Cost of the items after the discount for a specific order number (in this case I need to display for order number 2). I can't get it to display this information.

Here is my code for creating the procedure with a cursor so far.

CREATE OR REPLACE PROCEDURE ComputeOrderTotal
(no_id IN orders.o_id%TYPE,
cfirst IN customer.c_first%TYPE,
clast IN customer.c_last%TYPE,
TotalNetCost OUT orders.ordertotal%TYPE) IS

CURSOR OrderCursor IS
  SELECT order_line.inv_id, inv_price, ol_quantity, inv_price*ol_quantity AS ExtPrice,
  CASE
    WHEN inv_price*ol_quantity >= 100 THEN 0.9*(inv_price*ol_quantity)
    WHEN inv_price*ol_quantity >= 10 THEN 0.95*(inv_price*ol_quantity)
    ELSE
    inv_price*ol_quantity
    END AS NetCost
  FROM inventory, order_line, orders, customer
  WHERE orders.o_id = customer.c_id;
OrderRow OrderCursor%ROWTYPE;

BEGIN
OPEN OrderCursor;
LOOP 
  FETCH OrderCursor INTO OrderRow;
  EXIT WHEN OrderCursor%NOTFOUND;
    TotalNetCost :=TotalNetCost + OrderRow.NetCost;  

END LOOP; 


DBMS_OUTPUT.PUT_LINE('Order Number:  ' || no_id || 'First Name: ' || cfirst  || 'Last Name:  ' ||
clast || 'Total Net Cost: ' || TO_CHAR(TotalNetCost, '$0999.99'));

END;

And here is my code for calling the procedure.

DECLARE
no_id orders.o_id%TYPE;
cfirst customer.c_first%TYPE;
clast customer.c_last%TYPE;
TotalNetCost orders.ordertotal%TYPE;

BEGIN
ComputeOrderTotal(2, cfirst, clast, TotalNetCost);    
END;

Thanks for the help!

i am not sure but your conditions could be the problem as your second condition contradicts the first. for example 110 satisfy the first condition but it also satisfy the second condition.

WHEN inv_price*ol_quantity >= 100 THEN 0.9*(inv_price*ol_quantity)
WHEN inv_price*ol_quantity >= 10 THEN 0.95*(inv_price*ol_quantity)

i would suggest changing second condition to <100 and >=10

Add this line to the top of the code calling the procedure.

Alter session set serveroutput on;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM