Get the App
SLTechnology News&Howtos  ›  Database  › 

How mysql creates stored functions

Shulou Source: shulou.com Published: 2022-06-01 03:25:04 09月18日 Update

This article is about how mysql creates storage functions. Xiao Bian thinks it is quite practical, so share it for everyone to make a reference. Let's follow the editor and have a look.

A stored function is a snippet of code consisting of SQL statements and procedural statements that can be called by applications and other SQL statements. In MySQL, a stored function can be created by the CREATE FUNCTION statement.

Storage function is a special type of storage program that returns a single value. Next, we will introduce in detail how to create a storage function in MySQL, which has certain reference value and hopes to help everyone.

stored function

Stored functions are similar to stored procedures in that they are code snippets composed of SQL statements and procedural statements that can be invoked by applications and other SQL statements. Since the stored function itself is an output function, it cannot have output parameters, and it can be called directly without a call statement.

Creation of Storage Functions

syntax

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

Syntax analysis:

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

(param1,param2,…): Represents all parameters of the stored function, which by default are IN parameters. You cannot specify IN, OUT, or INOUT modifiers for parameters.

RETURNS datatype: indicates the data type of the return value, which can be any valid MySQL datatype

[NOT]TERMINISTIC: Indicates that the result is indeterminate and that the same input may yield different outputs. If no value is specified, the default is [NOT] DETERMINATION

SQL: Program body

Example: Create a stored function named demo that returns the query result of a SELECT statement with a string type value. 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 ; Thank you for reading! About mysql how to create a storage function to share here, I hope the above content can be of some help to everyone, so that everyone can learn more knowledge. If you think the article is good, you can share it so that more people can see it!

Tags: Functions storage statements parameters programs types code output content that is applications data articles are made by more fragments programs results syntax references Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Microsoft MySQL NVidia MariaDB