Home > Database > Mysql Tutorial > How to create a stored function in mysql

How to create a stored function in mysql

清浅
Release: 2020-09-14 17:08:21
Original
7381 people have browsed it

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
Copy after login

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  ;
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template