Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use mysql Storage function

Shulou Source: shulou.com Published: 2022-06-01 03:26:19 11月04日 Update

This article is to share with you about the use of mysql storage functions. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.

What is a storage function:

Encapsulate a piece of sql code, complete a specific function, and return the result.

Syntax for storing functions:

Create function function ([function arguments],... .]) Returns return type Begin If (Return) Else Return (returned data) end if; end

For example:

Create function count_news (hits int) returns int

Unlike the stored procedure return parameters, the stored function does not directly declare which variable is the return parameter when it is defined, but only uses returns to declare the data type to which the return parameter belongs, and the return parameter is expressed in the function body in the form of the data variable returned by return. What we should pay attention to is:

The storage function only supports input parameters, and there is no IN or INOUT before input parameters.

Limitations in storage functions

Flow control (Flow-of-control) statements (IF, CASE, WHILE, LOOP, WHILE, REPEAT, LEAVE,ITERATE) are also legal.

Variable declaration (DECLARE) and assignment (SET) are legal.

Conditions are allowed to be declared.

Exception handling declarations are also allowed.

But keep in mind here that the function has restrictions: you cannot access the table in the function. Therefore, it is illegal to use the following statement in a function.

ALTER 'CACHE INDEX' CALL COMMIT CREATE DELETE DROP' FLUSH PRIVILEGES' GRANT INSERT KILL LOCK OPTIMIZE REPAIR REPLACE REVOKE ROLLBACK SAVEPOINT 'SELECT FROM table'' SET system variable' 'SET TRANSACTION' SHOW' START TRANSACTION' TRUNCATE UPDATE Thank you for reading! On the use of mysql storage functions to share here, I hope that the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.

Tags: Function storage parameter data variable usage method legality content is in more condition type statement input different good practical code function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Redmi OPPO Reno Shulou Tech Info Apple