什么是 MySQL 存储函数以及如何创建它们?

数据库数据库 2023-08-31 03:32:35 1165
摘要: Thisfeatureofstoredfunctionsisdifferentfromstoredprocedures.Actually,astoredfunctionparameterisequivalentoftheINparameterofthestoredprocedureasthefunctionsuseRETURNkeywor...

什么是 MySQL 存储函数以及如何创建它们?

This feature of stored functions is different from stored procedures. Actually, a stored function parameter is equivalent of the IN parameter of the stored procedure as the functions use RETURN keyword to determine what is passed back. Its syntax can be as follows −

Syntax

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

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

函数只有输入参数和返回值,因此在函数定义中必须有一个RETURNS子句来指示返回值的数据类型。此外,在函数体内必须至少有一个RETURN语句来将值返回给调用者。

以上就是什么是 MySQL 存储函数以及如何创建它们?的详细内容,更多请关注其它相关文章!