MySQL 存储过程与函数,我将在什么时候使用?

2021-11-20 00:00:00 mysql stored-procedures sql-function

我正在研究 MySQL 存储过程和函数.真正的区别是什么?

I'm looking at MySQL stored procedures and function. What is the real difference?

它们看起来很相似,但一个功能有更多的限制.

They seem to be similar, but a function has more limitations.

我可能错了,但似乎存储过程可以做任何事情,而存储函数可以做更多.为什么/什么时候我会使用过程还是函数?

I'm likely wrong, but it seems a stored procedure can do everything and more a stored function can. Why/when would I use a procedure vs a function?

推荐答案

存储过程不能和普通 SQL 混用,而存储函数可以.

You can't mix in stored procedures with ordinary SQL, whilst with stored function you can.

例如如果 get_foo() 是一个过程,则 SELECT get_foo(myColumn) FROM mytable 无效,但如果 get_foo() 是一个过程,你可以这样做功能.代价是函数比过程有更多的限制.

e.g. SELECT get_foo(myColumn) FROM mytable is not valid if get_foo() is a procedure, but you can do that if get_foo() is a function. The price is that functions have more limitations than a procedure.

相关文章