Home  >  Article  >  Database  >  What is the most significant difference between MySQL functions and procedures?

What is the most significant difference between MySQL functions and procedures?

WBOY
WBOYforward
2023-09-17 22:49:021246browse

MySQL 函数和过程之间最显着的区别是什么?

The most significant difference between procedures and functions is that they are called differently and for different purposes. Apart from this, following are the differences between procedures and functions -

  • Procedures do not return values. Instead, it is called using a CALL statement to perform an operation, such as modifying a table or processing retrieved records.

    Functions, on the other hand, are called within an expression and return a single value directly to the caller to be used in the expression. That is, functions can be used in expressions in the same way as constants, built-in functions, or references to table columns.

  • We cannot call functions using: CALL statement. We cannot call procedures within expressions.

  • The syntax for routine creation is somewhat different from procedures and functions, as follows-

CREATE
   [DEFINER = { user | CURRENT_USER }]
   PROCEDURE sp_name ([proc_parameter[,...]])
   [characteristic ...] routine_body

CREATE
   [DEFINER = { user | CURRENT_USER }]
   FUNCTION sp_name ([func_parameter[,...]])
   RETURNS type
   [characteristic ...] routine_body

proc_parameter:
   [ IN | OUT | INOUT ] param_name type

func_parameter:
   param_name type

type:
   Any valid MySQL data type

characteristic:
   COMMENT 'string'
   | LANGUAGE SQL
   | [NOT] DETERMINISTIC
   | { CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA }
   | SQL SECURITY { DEFINER | INVOKER }

routine_body:
   Valid SQL routine statement
  • Procedure parameters can be defined as Input only, output only, or both input and output. This means that the procedure can use output parameters to pass values ​​back to the caller. These values ​​can be accessed in statements following the CALL statement.

    On the other hand, functions only have input parameters. Therefore, although both procedures and functions can have parameters, the parameter declaration syntax for procedures is different from that for functions.

  • The function returns a value, so there must be a RETURNS clause in the function definition indicating the data type of the return value. Additionally, there must be at least one RETURN statement within the function body to return a value to the caller.

    On the other hand, RETURNS and RETURN do not appear in the procedure definition.

    li>

The above is the detailed content of What is the most significant difference between MySQL functions and procedures?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete