Home > database >  Encountered the symbol "=" when expecting one of the following:
Encountered the symbol "=" when expecting one of the following:

Time:12-13


Create or replace 
procedure price_ive
        (inv_id IN NUMBER,
        inv_price OUT NUMBER)
        IS PRICE NUMBER;
    BEGIN
        SELECT inv_price
            INTO PRICE
            FROM ct_inventory
            WHERE inv_id = 1;
        if inv_id >= 1 then 
            inv_price := 259.99;

        elsif inv_id >= 3 then 
            inv_price := 29.95;

        elsif inv_id >= 9 then 
            inv_price := 59.95;

        elsif inv_id >= 15 then 
            inv_price := 15.99;

        elsif inv_id >= 23 then 
            inv_price := 199.95;

        elsif inv_id >= 25 then 
            inv_price := 209.95;

        elsif inv_id >= 27 then 
            inv_price := 15.95;

        else inv_id >= 30 then 
             inv_price := 19.99;
        end if;
    end price_ive;

Currently using PL/SQL to find the inv_price for specific inv_id values. I am having issue where it says Encountered the symbol "=" when expecting one of the following:.

CodePudding user response:

The last else must also be elsif because you're still checking inv_id value.

SQL> Create or replace
  2  procedure price_ive
  3          (inv_id IN NUMBER,
  4          inv_price OUT NUMBER)
  5          IS PRICE NUMBER;
  6      BEGIN
  7          SELECT inv_price
  8              INTO PRICE
  9              FROM ct_inventory
 10              WHERE inv_id = 1;
 11          if inv_id >= 1 then
 12              inv_price := 259.99;
 13
 14          elsif inv_id >= 3 then
 15              inv_price := 29.95;
 16
 17          elsif inv_id >= 9 then
 18              inv_price := 59.95;
 19
 20          elsif inv_id >= 15 then
 21              inv_price := 15.99;
 22
 23          elsif inv_id >= 23 then
 24              inv_price := 199.95;
 25
 26          elsif inv_id >= 25 then
 27              inv_price := 209.95;
 28
 29          elsif inv_id >= 27 then
 30              inv_price := 15.95;
 31
 32          elsif inv_id >= 30 then       --> here
 33               inv_price := 19.99;
 34          end if;
 35      end price_ive;
 36
 37  /

Procedure created.

SQL>

CodePudding user response:

All of the elsif conditions in this code are useless, because if inv_id >= 1 then inv_price will be 259.99, and other condition checks will be skipped.

Try to rewrite the conditions sequence as

 11          if inv_id < 3 then
 12              inv_price := 259.99;
 13
 14          elsif inv_id < 9 then
 15              inv_price := 29.95;
 16
 17          elsif inv_id < 15 then
 18              inv_price := 59.95;
 19
 20          elsif inv_id < 23 then
 21              inv_price := 15.99;

and, finally:

             else -- no condition here
                 inv_price := 19.99;
  • Related