I can't create functions on SQL Developer

113 views Asked by At

I'm trying to create very basic function to learn.

CREATE OR REPLACE FUNCTION AddNumbers (
    num1 NUMBER,
    num2 NUMBER
) RETURN NUMBER
IS
    result NUMBER;
BEGIN
    result := num1 + num2;
    RETURN result;
END;

But this function wasn't compiling properly. Here is the error:

Error starting at line : 7 in command -
BEGIN
    result := num1 + num2;
    RETURN result;
END;
Error report -
ORA-06550: line 2, column 5:
PLS-00201: identifier 'RESULT' must be declared
ORA-06550: line 2, column 5:
PL/SQL: Statement ignored
ORA-06550: line 3, column 5:
PLS-00372: In a procedure, RETURN statement cannot contain an expression
ORA-06550: line 3, column 5:
PL/SQL: Statement ignored
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:

When I try to create a function, I always encounter this ORA-06550 error.

1

There are 1 answers

4
Littlefoot On

There's nothing wrong with code you posted, but I presume that you don't use SQL Developer properly. That error looks as if you selected executable part of function and ... well, executed it:

enter image description here

Don't select anything; just press CTRL + ENTER to create function (or 2nd button in toolbar, named "Execute script"; or press F5). Once you create it, then use it, e.g.

select addnumbers(1, 2) From dual;

Additional note: PL/SQL code should be terminated with a slash character. If you don't do that and try to run that SELECT (without selecting it), you'll get another error ("PLS-00103: Encountered the symbol "SELECT" ").


Demo:

SQL> CREATE OR REPLACE FUNCTION addnumbers (
  2      num1 NUMBER,
  3      num2 NUMBER
  4  ) RETURN NUMBER IS
  5      result NUMBER;
  6  BEGIN
  7      result := num1 + num2;
  8      RETURN result;
  9  END;
 10  /

Function created.

SQL> SELECT addnumbers(1, 2) FROM dual;

ADDNUMBERS(1,2)
---------------
              3

SQL>

SQL Developer screenshot:

enter image description here