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

WBOY
发布: 2023-08-24 21:57:02
转载
926 人浏览过

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

MySQL stored function is basically a special kind of stored program that returns a single value. We can use stored functions in MySQL to encapsulate mainly simple formulas or business rules that are reusable among SQL statements or stored programs. Other than that stored functions are used in SQL statements whenever an expression is used.

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 存储函数以及如何创建它们?的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!