PL/Sql 过程 vs 函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7272495/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 11:57:33  来源:igfitidea点击:

PL/Sql procedure vs function?

sqloracleplsql

提问by M Sach

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

我试图查看 pl/sql 过程和函数之间的区别,并找到了链接http://it.toolbox.com/blogs/oracle-guide/learn-plsql-procedures-and-functions-13030。首先让我告诉你开发人员通常使用 pl/sql 过程和函数做什么

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)

1)想得到一些返回值。他可以用函数和过程来实现它。如果他想返回单个值,他可以使用返回语句。如果他想返回多个值,他可以用 inout 参数来实现。同样,他可以从过程中获取带有 inout 参数的返回值(不是用 return 语句)

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.

但是只要他能够通过return语句或inout参数实现其意图,这对开发人员没有任何区别。

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.

2)他可以在Function和procedure中使用DML。所以在这里他也可以使用其中任何一个来更改数据库的状态。

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

我在某种程度上发现的唯一合理原因是函数可以从 SQL 调用,过程不能

Could somebody explain which one to use when and why?

有人可以解释一下何时以及为什么使用哪个吗?

采纳答案by Daniel Hilgarth

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.

您已经找到了主要区别。如果您想在 SQL 中使用它,您可以创建一个函数。当您只想在 PL/SQL 中使用它时,您创建了一个过程。

回答by Samuel

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".

此外,只有函数可能是“纯”(适用于函数索引)和“流水线”的。

回答by Sanjaya Pandey

There are main two different:

主要有两种不同:

1:Use Procedure to take some action. But use function to return some value. 
2:You can call function from sql query but Procedure can't. 
3:Best practice to use Procedure then function if possible. 

Thanks.

谢谢。

回答by Jay

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.

函数标题必须包含一个 RETURN 子句,用于指定返回值的数据类型。过程标题不能有 RETURN 子句。

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.

一个函数的可执行部分必须至少有一个 RETURN 语句。在过程中,RETURN 语句是可选的。有关详细信息,请参阅 RETURN 语句。

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#CHDDCFHD

回答by Dmitriy Buldakov

http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/subprograms.htm#i4079

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.

PL/SQL 子程序概述 PL/SQL 子程序是一个命名的 PL/SQL 块,可以用一组参数调用它。子程序可以是过程或函数。通常,您使用过程来执行操作,使用函数来计算和返回值。