MySQL creates a stored function (CREATE FUNCTION)

Stored functions, like stored procedures, are a collection of SQL statements defined in the database. A stored function can return a function value through a return statement, which is mainly used to calculate and return a value. The stored procedure does not directly return a value and is mainly used to perform operations.

In MySQL, use the CREATE FUNCTION statement to create a stored function, and its syntax is as follows:
CREATE FUNCTION sp_name ([func_parameter[...]])
RETURNS type
[characteristic ...] routine_body

in:

  • sp_name parameter: indicates the name of the stored function;
  • func_parameter: Indicates the parameter list of the storage function;
  • RETURNS type: Specifies the type of return value;
  • characteristic parameter: specify the characteristics of the stored function, the value of this parameter is the same as that of the stored procedure;
  • routine_body parameter: indicates the content of the SQL code, and BEGIN...END can be used to mark the beginning and end of the SQL code.

Note: When creating a function, the function name cannot be the same as the existing function name. In addition to the above requirements, the recommended function name (identifier) ​​is function_xxx or func_xxx.

func_parameter can consist of multiple parameters, where each parameter consists of a parameter name and a parameter type in the following form:
[ IN | OUT | INOUT ] param_name type

in:

  • IN means input parameters, OUT means output parameters, INOUT means both input and output;
  • The param_name parameter is the parameter name of the stored function;
  • The type parameter specifies the parameter type of the stored function, which can be any data type of the MySQL database.

Example 1

Use CREATE FUNCTION to create a function to query the name of a student in the tb_student table. The SQL statement and execution process are as follows:
mysql> USE test;
Database changed
mysql> DELIMITER //
mysql> CREATE FUNCTION func_student(id INT(11))
    -> RETURNS VARCHAR(20)
    -> COMMENT '查询某个学生的姓名'
    -> BEGIN
    -> RETURN(SELECT name FROM tb_student WHERE tb_student.id = id);
    -> END //
Query OK, 0 rows affected (0.10 sec)
mysql> DELIMITER ;

In the above code, the func_student function is created, which has a parameter id of type INT(11), and the return value is of type VARCHAR(20). The SELECT statement searches the tb_student table for the record whose id field value is equal to the id value of the parameter passed in, and returns the name field value of the record at the same time.

Creating a function is the same as creating a stored procedure, you need to DELIMITER //change , and finally DELIMITER ;change the end symbol to the default end symbol in the SQL statement through the command.

If a RETURN statement in a stored function returns a value of a type other than that specified in the function's RETURNS clause, the return value is coerced to the appropriate type. For example, if a function returns an ENUM or SET value, but the RETURN statement returns an integer, for the corresponding ENUM member of the SET member set, the value returned from the function is a string.

Extended reading

Since the operations of viewing, modifying, and deleting stored functions and stored procedures are almost the same, we will not explain how to operate stored functions in detail.

The syntax for viewing stored functions is as follows:
SHOW FUNCTION STATUS LIKE 存储函数名;
SHOW CREATE FUNCTION 存储函数名;
SELECT * FROM information_schema.Routines WHERE ROUTINE_NAME=存储函数名;

It can be found that the difference between operating stored functions and operating stored procedures is that PROCEDURE is replaced by FUNCTION. Likewise, the syntax for modifying a stored function is as follows:

ALTER FUNCTION 存储函数名 [ 特征 ... ];

The characteristics of stored functions are basically the same as those of stored procedures.

The syntax for deleting a stored procedure is as follows:

DROP FUNCTION [ IF EXISTS ] <函数名>;

Guess you like

Origin blog.csdn.net/weixin_56175092/article/details/130371892