Can't create any function in Oracle SQL Developer

82 views Asked by At

I'm learning PL/SQL. When I try to create a function, it doesn't matter regardless of content I get errors. This is the function I wrote as an example:

CREATE OR REPLACE FUNCTION f_sum(num1 NUMBER, num2 NUMBER)
RETURN NUMBER
IS
    res NUMBER :=0;
BEGIN
    res:= num1 + num2;
    RETURN res;
END;
/

This is the error message:

Functıon F_SUM created.


Error starting at line : 5 in command -
BEGIN
    res:= num1 + num2;
    RETURN res;
END;
Error report -
ORA-06550: line 2, column 5:
PLS-00201: identifier 'RES' 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:

After running the script, function is created as follows:

create or replace FUNCTION f_sum(num1 NUMBER, num2 NUMBER)
RETURN NUMBER
IS
    res NUMBER :=0

Then I try to correct function manually. It works fine when I do this but I don't think this way is correct. For your information I tried pressing F5 and ctrl+enter but none of them seems to be working properly.

1

There are 1 answers

2
Littlefoot On

Function seems to be OK and it works as expected:

enter image description here

This error line:

PLS-00372: In a procedure, RETURN statement cannot contain an expression

suggests you executed only this part of code (SQL*Plus illustration):

SQL> BEGIN
  2      res:= num1 + num2;
  3      RETURN res;
  4  END;
  5  /

which results in error you got:

    res:= num1 + num2;
    *
ERROR at line 2:
ORA-06550: line 2, column 5:
PLS-00201: identifier 'RES' 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


SQL>

Basically, it says that procedure's return should be just that: return;.

The question is: did you, by any chance, "select" (mark) only that piece of code while executing it? If so, don't select anything.

enter image description here