Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Compilation error in Stored Procedure (Oracle SQL)

create or replace function getAvg(id1 IN number, id2 IN number) return number as
sal1 number;
sal2 number;
avg number;
BEGIN
    select esal into sal1 from employees where eno = id1;
    select esal into sal2 from employees where eno = id2;
    avg := (sal1+sal2)/2;
    return avg;
END;
/

When I try to compile the above code, I get compilation errors with following message:

Warning: Function created with compilation errors.

But when I replace avg after return with (sal1+sal2)/2 it compiles successfully.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

That’s bad habits: never name your own objects, variables, whatever using reserved words or keywords. avg is a built-in function; rename the variable:

SQL> create or replace function getAvg(id1 IN number, id2 IN number)
  2    return number
  3  as
  4    sal1 number;
  5    sal2 number;
  6    l_avg number;
  7  BEGIN
  8      select esal into sal1 from employees where eno = id1;
  9      select esal into sal2 from employees where eno = id2;
 10      l_avg := (sal1+sal2)/2;
 11      return l_avg;
 12  END;
 13  /

Function created.

SQL> select * from employees;

       ENO       ESAL
---------- ----------
         1        100
         2        200

SQL> select getavg(1, 2) from dual;

GETAVG(1,2)
-----------
        150

SQL>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading