Home>Article>Database> How to create a stored function in mysql

How to create a stored function in mysql

Mary-Kate Olsen
Mary-Kate Olsen Original
2019-04-30 16:07:38 7267browse

Mysql method to create a stored function: [CREATE FUNCTION function_name(param1) RETURNS datatype DETERMINISTIC statements SQL]. A stored function is itself an output function, so it cannot have output parameters.

How to create a stored function in mysql

[Recommended course:mysql video tutorial]

Storage function

Stored functions are very similar to stored procedures. They are code fragments composed of SQL statements and procedural statements, and can be called by applications and other SQL statements. Since the storage function itself is an output function, it cannot have output parameters. In addition, the storage function can be called directly without a call statement.

How to create a stored function in mysql

Creation of storage function

Grammar

CREATE FUNCTION function_name(param1,param2,…) RETURNS datatype [NOT] DETERMINISTIC statements SQL

Grammar analysis :

CREATE FUNCTION clause is followed by the name of the specified stored function

(param1,param2,…) :Represents all parameters of the stored function. By default, all The parameters are all IN parameters. The IN, OUT or INOUT modifier cannot be specified for a parameter.

RETURNS datatype: Indicates: the data type of the return value, it can be any valid MySQL data type

[NOT] DETERMINISTIC: Indicates that the result is undefined, the same input may get different results Output. If no value is specified, the default is [NOT] DETERMINISTIC

SQL: Program body

Example: Create a stored function named demo, which returns the query of the SELECT statement As a result, the numeric type is string. The code is as follows:

mysql> DELIMITER // mysql> CREATE FUNCTION demo() -> RETURNS CHAR(50) -> RETURN ( SELECT s_name FROM suppliers WHERE s_call='48075'); -> // Query OK, 0 rows affected (0.11 sec) mysql> DELIMITER ;

The above is the detailed content of How to create a stored function in mysql. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn