我试图研究 pl/sql 过程和函数之间的区别,并找到链接 http://it.toolbox.com/blogs/oracle-guide/learn-plsql-procedures-and-functions-13030。首先让我告诉你开发人员通常使用 pl/sql 过程和函数做什么
1)想要获得一些返回值。他可以使用函数和过程来实现它。对于函数,如果他想返回单个值,他可以使用 return 语句。如果他想返回多个值,他可以通过 inout 参数来实现。同样,他可以从 procedure 中使用 inout 参数来获取返回值(而不是通过 return 语句),
但这对开发人员来说没有任何区别,只要他能够实现它意图与 return 语句或 inout 参数。
所以这里两者也可以互相替换。
2)他可以在函数和过程中使用DML。所以在这里他也可以使用其中任何一个来改变数据库的状态。
所以我没有得到任何具体的推理,因为两者可以在某些方面相互替换。
我在某种程度上发现的唯一合理的原因是函数可以从 SQL 调用,过程
不能有人解释一下何时使用哪一个以及为什么使用?
I tried to looked in to difference between pl/sql procedure and function and found the link http://it.toolbox.com/blogs/oracle-guide/learn-plsql-procedures-and-functions-13030. First let me tell you what a developer generally do with pl/sql procedure and function
1) Wanted to get the some return value. He can acieve it with both function and procedure .With function if he want to return a single value he can use return statement . If he want to return multiple values he can achieve it with inout parameter.Similarily he can get return value with inout parameter from procedure(not with return statement)
But it does not make any difference to developer as long as he is able to achieve its intentention either with return statement or inout parameter.
so here also both can replace each other.
2) He can use DML in both Function and procedure. So here also he can use either of these to change the state of databse.
So i dont get any concrete reasoning which one to use where as both can replace each other in some.
The only reasonable reason i found up to some extent is that Functions can be called from SQL, procedure cannot
Could somebody explain which one to use when and why?
发布评论
评论(5)
过程和函数具有相同的结构,不同之处在于:
函数标题必须包含指定返回值的数据类型的 RETURN 子句。过程标题不能有 RETURN 子句。
函数的可执行部分必须至少有一个 RETURN 语句。在过程中,RETURN 语句是可选的。有关详细信息,请参阅 RETURN 语句。
欲了解更多信息,请参阅:
http://docs.oracle.com/cd/B28359_01 /appdev.111/b28370/subprograms.htm#CHDDCFHD
A procedure and a function have the same structure, except that:
A function heading must include a RETURN clause that specifies the data type of the return value. A procedure heading cannot have a RETURN clause.
A function must have at least one RETURN statement in its executable part. In a procedure, the RETURN statement is optional. For details, see RETURN Statement.
For more information refer to:
http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/subprograms.htm#CHDDCFHD
http://docs.oracle.com/cd/B28359_01 /appdev.111/b28370/subprograms.htm#i4079
概述PL/SQL 子程序
PL/SQL 子程序是一个命名的 PL/SQL 块,可以使用一组参数来调用。子程序可以是过程或函数。 通常,您使用过程来执行操作,使用函数来计算并返回值。
http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/subprograms.htm#i4079
Overview of PL/SQL Subprograms
A PL/SQL subprogram is a named PL/SQL block that can be invoked with a set of parameters. A subprogram can be either a procedure or a function. Typically, you use a procedure to perform an action and a function to compute and return a value.
您已经发现了主要区别。如果您想在 SQL 中使用函数,则可以创建一个函数。当您只想在 PL/SQL 中使用过程时,您可以创建过程。
You already found the main difference. You create a function if you want to use it in SQL. You create a procedure, when you want to use it only in PL/SQL.
我做什么。如果没有副作用,则使用函数,否则使用过程。
此外,只有函数可以是“纯函数”(适合函数索引)和“流水线”。
What I do. Use functions if there aren't side effects, procedures otherwise.
Moreover, only functions may be "pure"(suitable for function indexes) and "pipelined".
主要有两个不同:
谢谢。
There are main two different:
Thanks.