Question: Using PL/SQL, create the exercises below Below is a function that will calculate the total discount based on an order ID. Using the code as

Using PL/SQL, create the exercises below

Below is a function that will calculate the total discount based on an order ID. Using the code as a starting template, modify it so that it will return the actual percentage of savings by also calculating the TotalCost and then dividing TotalDiscount by TotalCost to get the percent saved. ?You must calculate TotalCost in the same manner that TotalDiscount is being calculated below, e.g. use the Cursor and variables in the loop.

CREATE OR REPLACE FUNCTION getTotalDiscount

(o_id IN NUMBER)

return NUMBER

IS

TotalDiscount NUMBER := 0;

CURSOR getOrderItems IS

SELECT DISCOUNT_AMOUNT FROM order_items WHERE ORDER_ID = o_id;

BEGIN FOR items IN getOrderItems LOOP

TotalDiscount := TotalDiscount + items.DISCOUNT_AMOUNT;

END LOOP; RETURN TotalDiscount;

EXCEPTION WHEN OTHERS THEN

dbms_output.put_line('Something went wrong...');

END

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!