无法在 Oracle SQL Developer 中创建任何函数

问题描述 投票:0回答:1

我正在学习 PL/SQL。当我尝试创建一个函数时,无论内容如何,我都会收到错误,这并不重要。这是我作为示例编写的函数:

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

这是错误消息:

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:

运行脚本后,创建函数如下:

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

然后我尝试手动纠正功能。当我这样做时效果很好,但我认为这种方法不正确。供您参考,我尝试按 F5 和 ctrl+enter 但它们似乎都无法正常工作。

oracle plsql oracle-sqldeveloper plsqldeveloper
1个回答
1
投票

功能似乎没问题,并且按预期工作:

此错误行:

PLS-00372:在过程中,RETURN 语句不能包含表达式

建议您只执行这部分代码(SQL*Plus 插图):

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

这会导致你得到错误:

    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>

基本上,它说procedure

return
应该是:
return;

问题是:您是否在执行时仅“选择”(标记)了那段代码?如果是这样,请不要选择任何内容

© www.soinside.com 2019 - 2024. All rights reserved.