英文:
differences between function and procedures in oracle
问题
I know that this question is all over..
但我真的很难理解,
我在各处看到,函数不能用来执行数据库的CRUD操作(比如更新语句),这是不正确的。
除此之外,它们之间的主要区别基本上是,存储过程可以具有0个到n个输入和输出参数,但函数具有返回值,不会将值存储在输出参数中。
是的,还有一些小的区别,比如你调用它的方式,如果我没记错,函数可以在select语句中调用,而存储过程不能。
所以说实话,我真的看不出这两者之间有什么区别。
如果有人问我“为什么你会选择函数而不是存储过程”(或反之),我该怎么回答。谢谢。
英文:
I know that this question is all over..
But Im really struggling to understand,
I see everywhere that functions cannot be used to perform crud operations on the db ( such as update statement ) which is not true.
Other than that, Basically the MAIN difference between them is that a procedure can have in and out parameters 0->n but a function has a return, and does not store the value in an out parameter..
Yea there are some small differences like the way you invoke it, if Im not wrong a function can be called within a select statement whereas a procedure cannot.
So to be honest, I dont really see any difference bewteen those two.
What should I answer if Im asked " Why would you choose function over procedure" ( or the opposite ). THANKS
答案1
得分: 1
以下是您要翻译的内容:
Well, that's true (except for a typo, here):
function can be called within a select statement whereas a function cannot (bold part should be "procedure")
你说得对(除了一个拼写错误):
函数可以在select语句内调用,而函数不行(粗体部分应该是“过程”)
You'd choose function when there's something you want to return to the caller. Use a procedure when you want to process something.
当你想要将结果返回给调用者时,你会选择函数。当你想要处理某些东西时,使用过程。
Functions - as you said - can be used in a select
statement, which can be used in both SQL and PL/SQL. Procedures, on the other hand, require PL/SQL. It is way simpler to call a function than a procedure (presume f_today
and p_today
return sysdate; function as return value, procedure via its out parameter), e.g.
select f_today from dual;
than
declare
l_today date;
begin
p_today(l_today);
dbms_output.put_line(l_today);
end;
/
正如你所说,函数可以在select
语句中使用,可以在SQL和PL/SQL中使用。而过程则需要PL/SQL。调用函数比调用过程简单得多(假设f_today
和p_today
分别返回sysdate;函数通过返回值返回,过程通过其输出参数返回),例如:
select f_today from dual;
而不是
declare
l_today date;
begin
p_today(l_today);
dbms_output.put_line(l_today);
end;
/
Functions can execute DML operations, but only if they are autonomous transactions. That's not what you'd always want to do. If you want to perform DML, you - usually - pick a procedure.
函数可以执行DML操作,但只有在它们是自主事务时才能执行。这通常不是你想要做的。如果你想执行DML操作,通常会选择过程。
Although it is possible to do probably everything in both of them, choose the one that is most appropriate for what you are currently doing. Sometimes it is a function, another time it is a procedure (and sometimes you create a function which is then called by the procedure).
尽管在它们两者中都可以做几乎一切,但选择最适合你当前正在做的工作的那个。有时它是一个函数,另一次它是一个过程(有时你创建一个函数,然后由过程调用它)。这只取决于情况。
英文:
Well, that's true (except for a typo, here):
> function can be called within a select statement whereas a function cannot (bold part should be "procedure")
You'd choose function when there's something you want to return to caller. Use a procedure when you want to process something.
Functions - as you said - can be used in a select
statement, which can be used in both SQL and PL/SQL. Procedures, on the other hand, require PL/SQL. It is way simpler to call a function than a procedure (presume f_today
and p_today
return sysdate; function as return value, procedure via its out parameter), e.g.
select f_today from dual;
than
declare
l_today date;
begin
p_today(l_today);
dbms_output.put_line(l_today);
end;
/
Functions can execute DML operations, but only if they are autonomous transactions. That's not what you'd always want to do. If you want to perform DML, you - usually - pick a procedure.
Although it is possible to do probably everything in both of them, choose the one that is most appropriate for what you are currently doing. Sometimes it is a function, another time it is a procedure (and sometimes you create a function which is then called by the procedure).
It just depends.
答案2
得分: 0
因为过程可以具有OUT参数,它们也可以向调用者返回一个值。而且因为函数在内部是可以执行与过程相同的操作的PL/SQL块,它们也可以处理事务。在它们之中都可以进行DML操作。
我认为真正的区别在于函数必须具有一个且仅有一个主要返回值(通过RETURN子句返回),而过程不需要返回任何内容,实际上无法使用RETURN子句返回。但是两者都可以使用OUT参数来传递信息。
当然,只有函数可以在SQL中调用(而不是PL/SQL)。但除此之外,你几乎可以使用函数或过程来完成任何需要的操作。我认为在函数的主要目的是检索单个原子值时使用函数是合理的,而在主要目的是对数据库进行更改或需要传递多个项目而没有一个项目在概念上是“主要”时,使用过程是合适的。
不过,让我进一步说明。在我的PL/SQL编程中,即使主要目的不是要获取返回值,我通常更倾向于使用函数,因为我几乎总是希望得到一个返回/状态代码(成功与失败),因此状态是几乎任何操作的良好“主要返回值”,这使函数非常合适。这种编程风格处理异常靠近其源并从命名函数中返回状态,而不是从函数中引发异常。但这只是一种方法。无论如何,希望这有所帮助。
英文:
Because procedures can have OUT parameters, they too can return a value to the caller. And because functions are internally PL/SQL blocks that can do everything procedures can do, they also process things. And DML is just as possible in either of them.
I think the real difference is that a function must have one and only one main return value (passed back by the RETURN clause), whereas a procedure does not have to return anything, and indeed cannot do so with a RETURN clause. But either can use OUT parameters to pass back information.
Of course only functions can be called in SQL (as opposed to PL/SQL). But besides that, you can pretty much do anything you need with either a function or a procedure. I think it makes good programming sense to use a function when the main point of it is to retrieve a single atomic value, and to use a procedure when the main point is to make changes to the database or there are multiple things that are to be passed back and no one item is conceptually the "main" thing.
However, let me further qualify. In my PL/SQL programming, I tend to use functions more often than procedures even if the main point isn't to get something back, because I nearly always want a return/status code back (success vs. failure), so status is a good "main return value" for just about anything, making functions a good fit. This style of programming handles exceptions close to their source and returns statuses from named functions rather than raising exceptions out of them. But this is only one way to do it. Anyway, I hope this helps.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论