[英]What is the difference between function and procedure in PL/SQL?
PL / SQL中的函数和过程有什么区别?
A procedure does not have a return value, whereas a function has. 过程没有返回值,而函数有。
Example: 例:
CREATE OR REPLACE PROCEDURE my_proc
(p_name IN VARCHAR2 := 'John') as begin ... end
CREATE OR REPLACE FUNCTION my_func
(p_name IN VARCHAR2 := 'John') return varchar2 as begin ... end
Notice how the function has a return clause between the parameter list and the "as" keyword. 注意函数如何在参数列表和“as”关键字之间有一个return子句。 This means that it is expected to have the last statement inside the body of the function read something like:
这意味着它应该在函数体内的最后一个语句读取如下内容:
return(my_varchar2_local_variable);
Where my_varchar2_local_variable is some varchar2 that should be returned by that function. 其中my_varchar2_local_variable是应该由该函数返回的一些varchar2。
A function can be in-lined into a SQL statement, eg 函数可以内联到SQL语句中,例如
select foo
,fn_bar (foo)
from foobar
Which cannot be done with a stored procedure. 使用存储过程无法做到这一点。 The architecture of the query optimiser limits what can be done with functions in this context, requiring that they are pure (ie the same inputs always produce the same output).
查询优化器的体系结构限制了在此上下文中可以对函数执行的操作,要求它们是纯的(即,相同的输入始终生成相同的输出)。 This restricts what can be done in the function, but allows it to be used in-line in the query if it is defined to be "pure".
这限制了函数中可以执行的操作,但如果将其定义为“纯”,则允许在查询中将其用于内联。
Otherwise, a function (not necessarily deterministic) can return a variable or a result set. 否则,函数(不一定是确定性的)可以返回变量或结果集。 In the case of a function returning a result set, you can join it against some other selection in a query.
在函数返回结果集的情况下,您可以将其与查询中的某些其他选择连接。 However, you cannot use a non-deterministic function like this in a correlated subquery as the optimiser cannot predict what sort of result set will be returned (this is computationally intractable, like the halting problem).
但是,您不能在相关子查询中使用这样的非确定性函数,因为优化器无法预测将返回哪种结果集(这在计算上是难以处理的,如停止问题)。
Both stored procedures and functions are named blocks that reside in the database and can be executed as and when required. 存储过程和函数都是驻留在数据库中的命名块,可以在需要时执行。
The major differences are: 主要区别是:
A stored procedure can optionally return values using out parameters, but can also be written in a manner without returning a value. 存储过程可以选择使用out参数返回值,但也可以以不返回值的方式写入。 But, a function must return a value.
但是,函数必须返回一个值。
A stored procedure cannot be used in a SELECT statement whereas a function can be used in a SELECT statement. 存储过程不能在SELECT语句中使用,而函数可以在SELECT语句中使用。
Practically speaking, I would go for a stored procedure for a specific group of requirements and a function for a common requirement that could be shared across multiple scenarios. 实际上,我会针对特定需求组和针对可在多个场景之间共享的常见需求的函数进行存储过程。 For example: comparing between two strings, or trimming them or taking the last portion, if we have a function for that, we could globally use it for any application that we have.
例如:比较两个字符串,或修剪它们或取最后一部分,如果我们有一个函数,我们可以全局地将它用于我们拥有的任何应用程序。
The following are the major differences between procedure and function, 以下是程序和功能之间的主要区别,
In dead simple way it makes this meaning. 以简单的方式,它就是这个意思。
Functions : 功能 :
These subprograms return a single value ; 这些子程序返回单个值 ; mainly used to compute and return a value.
主要用于计算和返回一个值。
Procedure : 程序:
These subprograms do not return a value directly; 这些子程序不直接返回值 ; mainly used to perform an action.
主要用于执行动作。
Example Program: 示例程序:
CREATE OR REPLACE PROCEDURE greetings
BEGIN
dbms_output.put_line('Hello World!');
END ;
/
Executing a Standalone Procedure : 执行独立过程:
A standalone procedure can be called in two ways: 可以通过两种方式调用独立过程:
• Using the EXECUTE
keyword • Calling the name of procedure from a PL/SQL block •使用
EXECUTE
关键字•从PL / SQL块调用过程名称
The procedure can also be called from another PL/SQL block: 也可以从另一个PL / SQL块调用该过程:
BEGIN
greetings;
END;
/
Function: 功能:
CREATE OR REPLACE FUNCTION totalEmployees
RETURN number IS
total number(3) := 0;
BEGIN
SELECT count(*) into total
FROM employees;
RETURN total;
END;
/
Following program calls the function totalCustomers
from an another block 以下程序从另一个块调用函数
totalCustomers
DECLARE
c number(3);
BEGIN
c := totalEmployees();
dbms_output.put_line('Total no. of Employees: ' || c);
END;
/
In the few words - function returns something. 用几句话来说 - 函数返回一些东西。 You can use function in SQL query.
您可以在SQL查询中使用函数。 Procedure is part of code to do something with data but you can not invoke procedure from query, you have to run it in PL/SQL block.
过程是对数据执行某些操作的代码的一部分,但是您无法从查询中调用过程,您必须在PL / SQL块中运行它。
This is what the difference i found. 这就是我发现的差异。 Please let me know if any .
如果有的话请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.