Introduction to the difference between Mysql stored procedures and functions

A stored procedure is a collection of user-defined sql statements, tasks involving a specific table or other object, the user can call a stored procedure, and a function is usually a database-defined method that accepts parameters and returns a value of some type and does not Involves a specific user table. 

There are several differences between stored procedures and functions: 

1) Generally speaking, the functions implemented by stored procedures are more complex, while the functions implemented by functions are more targeted. Stored procedures are powerful and can perform a series of database operations including modifying tables; user-defined functions cannot be used to perform a set of operations that modify the global database state.

2) For stored procedures, parameters can be returned, such as recordsets, while functions can only return values ​​or table objects. Functions can only return one variable; stored procedures can return multiple. The parameters of the stored procedure can be of three types: IN, OUT, and INOUT, while the function can only have the IN class. The return type is not required when the stored procedure is declared, and the return type needs to be described when the function is declared, and the function body must contain a valid the RETURN statement.

3) Stored procedures, non-deterministic functions can be used, and non-deterministic functions are not allowed to be built in the user-defined function body.

4) Stored procedures are generally executed as a separate part (EXECUTE statement execution), while functions can be called as part of a query statement (SELECT call), since a function can return a table object, it can be used in the query statement After the FROM keyword. Stored procedures are not available in SQL statements, instead functions can be used.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325480755&siteId=291194637