CREATE OR REPLACE FUNCTION FN_MULTAS_TOTAL
RETURN INTEGER
IS
V_TOTAL INT;
BEGIN
SELECT SUM(MULTA)
INTO V_TOTAL
FROM DETALLE_ARRIENDO;
RETURN V_TOTAL;
END;
我正在使用SQL Developer 11,我必须使用它来调用
exec FN_MULTAS_TOTAL;
并获得一个整数值
答案 0 :(得分:5)
此功能没问题:
SQL> create or replace function fn_multas_total
2 return integer
3 is
4 v_total int;
5 begin
6 select sum(multa)
7 into v_total
8 from detalle_arriendo;
9 return v_total;
10 end;
11 /
Function created.
但是,您不能用exec
来调用它-它在SQL * Plus或SQL Developer或其他支持它的工具中使用。这是匿名begin-end
PL / SQL块的“快捷方式”:
SQL> exec p_test
PL/SQL procedure successfully completed.
SQL> begin
2 p_test;
3 end;
4 /
PL/SQL procedure successfully completed.
它用于过程,而不是功能。你不能只是
SQL> begin
2 detalle_arriendo;
3 end;
4 /
detalle_arriendo;
*
ERROR at line 2:
ORA-06550: line 2, column 3:
PLS-00221: 'DETALLE_ARRIENDO' is not a procedure or is undefined
ORA-06550: line 2, column 3:
PL/SQL: Statement ignored
SQL>
这意味着您必须声明一个变量并将函数的结果提取到其中,例如
SQL> declare
2 l_result number;
3 begin
4 l_result := fn_multas_total;
5 end;
6 /
PL/SQL procedure successfully completed.
SQL>
通常,我们将函数用作
SQL> select fn_multas_total from dual;
FN_MULTAS_TOTAL
---------------
100
SQL>
如果您非常想使用exec
,则仍然必须声明一个变量,并且
SQL> var result number
SQL> exec :result := fn_multas_total;
PL/SQL procedure successfully completed.
SQL> print :result
RESULT
----------
100
SQL>