Detailed explanation of creating stored procedures and functions in mysql

Detailed explanation of creating stored procedures and functions in mysql

1. Stored Procedure

1.1. Basic Syntax

create procedure name ([params])

UNSIGNED [characteristics] routine_body

params: in|out|inout specifies the parameter list representing input and output

Routine_body: SQL code content, beginning with "begin" and ending with "end".

characteristics: specifies the characteristics of the stored procedure, including 5 types

1 DETERMINISTIC
2 NO SQL No SQL statements, and of course no data modification
3 READS SQL DATA only reads data, and of course does not modify the data
4 MODIFIES SQL DATA To modify data
5 CONTAINS SQL contains SQL statements

1.2 Create a stored procedure with specified execution permissions

create DEFINER=`root`@`%` procedure name ([params])

UNSIGNED [characteristics] routine_body

DEFINER: Specifies who has the authority to execute.

1.3 Use of DELIMITER

"DELIMITER //" means setting the "//" symbol as the end word, because the default statement end in MySQL is a semicolon ';'. In order to avoid conflicts between stored procedures and MySQL statement symbols, DELIMITER is sometimes used to change the end word symbol, and it should be used in conjunction with end //;

Example: Create a stored procedure executed by the root account to output the length of a given string

DELIMITER //
CREATE definer=`root`@`%` PROCEDURE `avgFruitPrice`(
in f_string VARCHAR(200)
)
BEGIN
    select length(f_string);
END//

2. Create a function

Functions are created in the same way as stored procedures

Example

DELIMITER //
CREATE definer=`root`@`%` FUNCTION `my_length`(
f_string VARCHAR(200)
)
RETURNS INT(11)
UNSIGNED NO SQL
BEGIN
    return length(f_string);
END//

Note: There are three things to note when creating a function.

1. RETURNS: The return type must be specified

2. UNSIGNED NO SQL requires specifying the stored procedure feature

3.return: Return the required data

Errors encountered:

If the error message above is displayed, it means that the stored procedure characteristics are not specified.

In a stored procedure function, you can use the MySQL query result as its parameter: The statement is select .... into

begin

declare onename char(50) default'0';

declare twoname char(50);

select f_name, b_name into onename, twoname from t_user where id =1;

.......

end//

illustrate:

declare: variables defined inside stored procedures and functions

default: default value

This is the end of this article about creating stored procedures and functions in MySQL. For more relevant MySQL stored procedures and functions, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Thoroughly understand MySQL stored procedures and functions
  • Analysis of the difference between MySQL stored functions and stored procedures
  • Introduction to commonly used functions in MYSQL database
  • MySQL batch inserts data through function stored procedures
  • MySQL stored functions detailed introduction
  • MySQL detailed summary of commonly used functions
  • Comprehensive summary of mysql functions
  • How to use MYSQL functions

<<:  HTML meta explained

>>:  Introduction to the pitfalls of Linux high concurrency and performance optimization

Recommend

Native js implementation of magnifying glass component

This article example shares the specific code for...

React example showing file upload progress

Table of contents React upload file display progr...

MySQL table name case selection

Table of contents 1. Parameters that determine ca...

Linux file management command example analysis [display, view, statistics, etc.]

This article describes the Linux file management ...

Detailed explanation of root directory settings in nginx.conf

There are always some problems when configuring n...

Docker-compose tutorial installation and quick start

Table of contents 1. Introduction to Compose 2. C...

Common problems and solutions during MySQL MGR construction

Table of contents 01 Common Faults 1 02 Common Fa...

Use three.js to achieve cool acid style 3D page effects

This article mainly introduces how to use the Rea...

js date and time formatting method example

js date time format Convert the date and time to ...

Vue implements weather forecast function

This article shares the specific code of Vue to r...

How to use Docker Swarm to build WordPress

cause I once set up WordPress on Vultr, but for w...

Vue custom encapsulated button component

The custom encapsulation code of the vue button c...

Zabbix monitoring docker application configuration

The application of containers is becoming more an...

React antd tabs switching causes repeated refresh of subcomponents

describe: When the Tabs component switches back a...

Apache ab concurrent load stress test implementation method

ab command principle Apache's ab command simu...