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

How to configure Http, Https, WS, and WSS in Nginx

Written in front In today's Internet field, N...

How to modify the forgotten password when installing MySQL on Mac

1. Install MySQL database on mac 1. Download MySQ...

Docker+gitlab+jenkins builds automated deployment from scratch

Table of contents Preface: 1. Install Docker 2. I...

Summary of various forms of applying CSS styles in web pages

1. Inline style, placed in <body></body&g...

Introduction to Linux File Compression and Packaging

1. Introduction to compression and packaging Comm...

Solve the error "Can't locate ExtUtils/MakeMaker.pm in @INC"

When installing mha4mysql, the steps are roughly:...

How to connect to docker server using ssh

When I first came into contact with docker, I was...

Solve the margin: top collapse problem in CCS

The HTML structure is as follows: The CCS structu...

How to store images in MySQL

1 Introduction When designing a database, it is i...

Mysql some complex sql statements (query and delete duplicate rows)

1. Find duplicate rows SELECT * FROM blog_user_re...

Analysis of GTK treeview principle and usage

The GtkTreeView component is an advanced componen...

Reasons and solutions for prompting to save action after uploading files in form

The json data must be returned in html format That...