PL/SQL loop with while

Viewed 83

the problem is, the loop is not counting as need:

I need to count for example: a DVD price at 100% is equal with 64$ at 95% is 60.8$ at 90 it must take the original price (64$) and make 90% from this amount, but instead, he is counting from the last (60.8$) price and he is doing 90%.

create or replace procedure PR_PRICE
is V_PRICE DVD.AMOUNT_PRICE_DVD%type;
p_counter number(3):=100;
begin
    select AMOUNT_PRICE_DVD into V_PRICE
    from DVD
    where TITLE_DVD like '%pop';
 --   p_counter :=100;

    while p_counter >= 80 loop
        V_PRICE := p_counter*V_PRICE/ 100;

        dbms_output.put_line('The price of DVD at ' || p_counter ||'% is: ' ||  V_PRICE ||'$');
        p_counter := p_counter-5;
    end loop;


end PR_PRICE;
/
1 Answers

Instead of overriding v_price, create a different variable for the discounted price:

create or replace procedure PR_PRICE
is 
V_PRICE DVD.AMOUNT_PRICE_DVD%type;
V_DISCOUNT_PRICE DVD.AMOUNT_PRICE_DVD%type; -- Declared here
p_counter number(3):=100;
begin
    select AMOUNT_PRICE_DVD into V_PRICE
    from DVD
    where TITLE_DVD like '%pop';

    p_counter :=100;

    while p_counter >= 80 loop
        V_DISCOUNT_PRICE := p_counter*V_PRICE/ 100; -- Calculated here

        dbms_output.put_line('The price of DVD at ' || p_counter ||'% is: ' ||  V_DISCOUNT_PRICE ||'$');
        p_counter := p_counter-5;
    end loop;


end PR_PRICE;
Related