MYSQL stored procedures, that is, a summary of common logical knowledge points

MYSQL stored procedures, that is, a summary of common logical knowledge points

Mysql stored procedure

1. Create stored procedure syntax (format)

DELIMITER $
CREATE PROCEDURE stored procedure name A (IN incoming parameter name a INT, IN incoming parameter name b VARCHAR(20), OUT return parameter name c INT)
BEGIN
  content..........
END $

Analysis:

  • IN represents the incoming parameter, defines the incoming parameter name, and is followed by the incoming parameter type (INT, VARCHAR, DOUBLE, .....)
  • OUT represents the data returned after the stored procedure is executed. Define the parameter name and follow it with the parameter type (INT, VARCHAR, DOUBLE, .....)
  • INOUT means that it can be passed in or returned. Define the parameter name and follow it with the parameter type (INT, VARCHAR, DOUBLE, .....)

2. Specific syntax and logic in stored procedures

A. Define variable syntax:

DECLARE variable name a parameter type (INT, VARCHAR (20), BOOLEAN, .....) [DEFAULT NULL];

Note: You can add DEFAULT NULL after the parameter type to set the initial value.

B. Variable assignment:

Method 1 (directly assigning values ​​to variables):

SET defined variable a = NEW();

Method 2 (the result of the SQL query is directly assigned to the variable):

SELECT `student`.age INTO defined variable a FROM `student` WHERE...........

Method 3 (the results of the SQL query are directly assigned to multiple variables):

SELECT `student`.name AS defined variable a,`student`.age AS defined variable b INTO defined variable a, defined variable b FROM `student` ...............

c. Logical reasoning:

#IF judgment: 

         IF conditional statement (3>5) THEN
        Execute if the condition is TRUE.........;
      END IF;
  #IF ELSE judgment:
      IF condition (a>0) THEN
     Execute when the condition is (a>0)........;
      ELSE IF condition (a<0) THEN
         Execute when the condition is (a<0).......;
      ELSE
         Other executions.......;
      END IF;

D. Cursor, (LOOP) loop:

#Example. Single cursor loop: create procedure my_procedure() -- Create a stored procedure begin -- Start a stored procedure declare my_id varchar(32); -- Custom variable 1
declare my_name varchar(50); -- Custom variable 2
DECLARE done INT DEFAULT FALSE; -- Custom control cursor loop variable, default false

DECLARE cur CURSOR FOR ( SELECT id, name FROM t_people ); -- Define the cursor and enter the result set DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; -- Bind the control variable to the cursor, and the cursor loop will automatically turn to true
 
OPEN cur; -- Open the cursor posLoop: LOOP -- Start the loop body, myLoop is the custom loop name, and use FETCH cur into my_id, my_name to end the loop; -- Assign the data sequence of the current row read by the cursor to the custom variable 12
    IF done THEN -- Determine whether to continue the loop LEAVE posLoop; -- End the loop END IF;
  -- To do what you want, you can directly use custom variables in SQL UPDATE t_user SET c_name = my_name WHERE id = my_id and rtrim(ltrim(c_name)) = ''; -- Remove spaces on the left and right COMMIT; -- Commit the transaction END LOOP posLoop; -- End the custom loop CLOSE cur; -- Close the cursor END; -- End the stored procedure
#Example. Multi-cursor loop: create procedure my_procedure() -- Create a stored procedure begin -- Start a stored procedure declare my_id varchar(32); -- Custom variable 1
declare my_name varchar(50); -- Custom variable 2
DECLARE done INT DEFAULT FALSE; -- Custom control cursor loop variable, default false

DECLARE cur_1 CURSOR FOR ( SELECT id, name FROM t_people ); -- Define the cursor and enter the result set DECLARE cur_2 CURSOR FOR ( SELECT id_2, name_2 FROM t_people_2); -- Define the cursor and enter the result set DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; -- Bind the control variable to the cursor, and the cursor loop will automatically turn to true when it ends
 
OPEN cur_1; -- Open cursor posLoop: LOOP -- Start the loop body, myLoop is the custom loop name, and use FETCH cur_1 into my_id, my_name to end the loop; -- Assign the data sequence of the current row read by the cursor to the custom variable 12
    IF done THEN -- Determine whether to continue the loop LEAVE posLoop; -- End the loop END IF;
  -- To do what you want to do, just use the custom variable in sql UPDATE ..........; --Specific operations you want to do END LOOP posLoop; --End the custom loop body CLOSE cur_1; --Close the cursor SET done = FALSE; --Because when the first cursor is traversed, its value is set to TRUE by the handler. If you don't use set to set it to FALSE, the second cursor will not traverse. (It is best to use this statement before each operation to open a cursor to ensure that the cursor can actually be traversed)
 
 OPEN cur_2; -- Open cursor posLoop_2: LOOP -- Start the loop body, myLoop is the custom loop name, and use FETCH cur_2 into my_id, my_name to end the loop; -- Assign the data sequence of the current row read by the cursor to the custom variable 12
    IF done THEN -- Determine whether to continue the loop LEAVE posLoop_2; -- End the loop END IF;
  -- To do what you want, you can directly use the custom variable in SQL INSERT ..........; --The specific operation you want to do END LOOP posLoop_2; --End the custom loop body CLOSE cur_2; --Close the cursor END; --End the stored procedure

3. Calling stored procedures

#Call a stored procedure that has no return value CALL stored procedure name (parameters.....);

#Call a stored procedure with a return value (get the return value)

CALL stored procedure name (@aaa);
SELECT @aaa;

4. Delete the stored procedure

DROP PROCEDURE stored procedure name;

5. Notes

The semicolon (;) in the stored procedure is very important and should not be omitted.

You may also be interested in:
  • In-depth understanding of Mysql logical architecture
  • MySQL Advanced Learning Notes (Part 3): Introduction to MySQL Logical Architecture, Detailed Explanation of MySQL Storage Engine
  • Detailed explanation of MySQL execution principle, logical layering, and changing database processing engine
  • Detailed explanation of Mysql logical architecture
  • Detailed explanation on how to avoid the pitfalls of replacing logical SQL in MySQL
  • Using PHP to access the MySql database logical operations and examples of adding, deleting, modifying and checking
  • Logical judgment and conditional control of MySql stored procedures
  • MySQL uses frm files and ibd files to restore table data
  • MySQL uses binlog logs to implement data recovery
  • MySQL quick recovery solution based on time point
  • Implementation of MySQL5.7 mysqldump backup and recovery
  • Summary of MySQL logical backup and recovery testing

<<:  Example of converting JavaScript flat array to tree structure

>>:  Setting up VMware vSphere in VMware Workstation (Graphic Tutorial)

Recommend

Detailed steps to install python3.7 on CentOS6.5

1. Download Python 3 wget https://www.python.org/...

Summary of HTML Hack Tags in IE Browser

Copy code The code is as follows: <!--[if !IE]...

Summary of common tool examples in MySQL (recommended)

Preface This article mainly introduces the releva...

MySQL 20 high-performance architecture design principles (worth collecting)

Open Source Database Architecture Design Principl...

Vue+axios sample code for uploading pictures and recognizing faces

Table of contents Axios Request Qs processing dat...

How to support full Unicode in MySQL/MariaDB

Table of contents Introduction to utf8mb4 UTF8 by...

Solution to the error problem of Vscode remotely connecting to Ubuntu

1. Background of the incident: Because of work ne...

Windows system mysql5.7.18 installation graphic tutorial

MySQL installation tutorial for Windows system do...

UTF-8 and GB2312 web encoding

Recently, many students have asked me about web p...

MySQL Series 8 MySQL Server Variables

Tutorial Series MySQL series: Basic concepts of M...

PNG Alpha Transparency in IE6 (Complete Collection)

Many people say that IE6 does not support PNG tra...

Use CSS3 to implement button hover flash dynamic special effects code

We have introduced how to create a waterfall layo...