SQL | PL/SQL中函数和存储过程之间的差异

先决条件:

null

PL/SQL中函数和存储过程的区别

存储过程(SP)和函数(用户定义函数(UDF))之间的差异:

1.SP 可能会也可能不会 返回一个值,但是 UDF 必须 返回一个值。函数的return语句将控制返回给调用程序,并返回函数的结果。 例子:

SP ->create or replace procedure GEEKS(x int) isy int;begin.....UDF->FUNCTION GEEKS(x int)  /*return statement so we must return value in function */  RETURN int             IS    y int;  BEGIN .....

2.SP 可以 输入/输出 参数但是 UDF 只有 具有输入参数。 例子:

 SP -> CREATE OR REPLACE PROCEDURE Factorial(x IN NUMBER, result OUT NUMBER) is begin ....UDF ->FUNCTION Factorial(x IN NUMBER) /* only input parameter */ return  NUMBERisresult NUMBER;begin.....

3. 我们可以打电话 UDF 从…起 服务提供商 但是 不能 呼叫 服务提供商 从一个函数。 例子:

Calling UDF cal() inside SP square() but reverse is not possible.set serveroutput on;declarea int;c int;function cal(temp  int)return intas ans int;beginans:=temp* temp;return ans;end;procedure square(x in int, ans out int) isbegindbms_output.put_line('calling function in procedure');ans:= cal(x);end;begina:=6;square(a, c);dbms_output.put_line('the answer is '|| c);end;OUTPUT: calling function in procedurethe answer is 36

4. 我们 不能 使用 服务提供商 在SQL语句中,比如SELECT、INSERT、UPDATE、DELETE、MERGE等,但我们可以将它们与UDF一起使用。

5. 在异常处理中,我们可以尝试使用SP 不能 在家里做 UDF .

6. 我们可以在 服务提供商 但这在中国是不可能的 UDF .

7. 我们可以考虑 UDF 作为一种表达,但在英语中是不可能的 服务提供商。

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享