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:
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:
|
<<: Example of converting JavaScript flat array to tree structure
>>: Setting up VMware vSphere in VMware Workstation (Graphic Tutorial)
Written in front In today's Internet field, N...
1. Install MySQL database on mac 1. Download MySQ...
Table of contents Preface: 1. Install Docker 2. I...
1. Inline style, placed in <body></body&g...
1. Introduction to compression and packaging Comm...
When installing mha4mysql, the steps are roughly:...
####Management of input and output in the system#...
aforementioned This article is very short~ The ma...
When I first came into contact with docker, I was...
The HTML structure is as follows: The CCS structu...
1 Introduction When designing a database, it is i...
1. Find duplicate rows SELECT * FROM blog_user_re...
In the UI cutting process, the page is often comp...
The GtkTreeView component is an advanced componen...
The json data must be returned in html format That...