Introduction to stored proceduresWhy use stored procedures?MySQL version 5.0 began to support stored procedures. Most SQL statements are single statements against one or more tables. Not all operations are that simple. Often a complete operation requires multiple statements to complete. Simply put, a stored procedure is a collection of one or more MySQL statements saved for later use. Think of it as a batch file. Their role is not limited to batch processing though.
Advantages of stored procedures
Disadvantages of stored procedures
Stored procedures in MySQLCreating and calling proceduresCreate a stored procedure, the code is as follows: -- Create a stored procedure create procedure mypro(in a int, in b int, out sum int) begin set sum = a+b; end; The results are as follows You can also view the process under the "Function" node in the Navicat client, as shown below: Call the stored procedure, the code is as follows: call mypro(1,2,@s);-- call the stored procedure select @s;-- display the procedure output results Operation Results Stored procedure syntax analysis
Parameters of stored procedures
Stored procedures can be divided into four categories based on parameters: 1). A procedure without parameters; 2). The process only has input parameters; 3). A process with only output parameters; 4). A procedure that contains input and output parameters. variableStored procedures in MySQL are similar to methods in Java. In this case, variables can also be used in stored procedures. The scope of local variables in Java is the method where the variable is located, while the scope of local variables in MySQL is the stored procedure where the variable is located. Variable Definition DECLARE variable_name [,variable_name...] datatype [DEFAULT value];
For example: declare name varchar(20) default 'jack'. Variable AssignmentSET variable name = expression value [,variable_name = expression ...] Use variables in the stored procedure, the code is as follows use schooldb;-- Use the schooldb database -- Create a procedure create procedure mypro1() begin declare name varchar(20); set name = 'Qiu Chuji'; select * from studentinfo where studentname = name; end; -- Call procedure call mypro1(); Operation Results Flow Control Statementsif conditional statement The Define a stored procedure, input an integer, and use an if statement to determine whether it is a positive or negative number. The code is as follows: -- Create procedure create procedure mypro2(in num int) begin if num<0 then -- condition starts selecting 'negative number'; elseif num=0 then select 'neither positive nor negative'; else select 'positive number'; end if;-- condition ends end; -- Call procedure call mypro2(-1); Operation Results case conditional statement Define a stored procedure, input an integer, and use the case statement to determine whether it is a positive or negative number. The code is as follows: -- Create procedure create procedure mypro3(in num int) begin case -- condition starts when num<0 then select 'negative number'; when num=0 then select 'neither positive nor negative'; else select 'positive number'; end case; -- condition ends end; -- Call procedure call mypro3(1); Operation Results Define a stored procedure, input an integer, and use a case statement to determine whether it is 1 or 2. The code is as follows: -- Create procedure create procedure mypro4(in num int) begin case num -- condition starts when 1 then select 'the value is 1'; when 2 then select 'the value is 2'; else select 'neither 1 nor 2'; end case; -- condition ends end; -- Call procedure call mypro4(3); Operation Results
While Loop Statement The usage of Define a stored procedure and use a while loop to output the cumulative sum from 1 to 10. The code is as follows: -- Create procedure create procedure mypro5(out sum int) begin declare num int default 0; set sum = 0; while num<10 do -- loop starts set num = num+1; set sum = sum+num; end while; -- end of loop end; -- Call procedure call mypro5(@sum); --Query variable value select @sum; Operation Results repeat loop statement The usage of the Define a stored procedure and use a repeat loop to output the cumulative sum from 1 to 10. The code is as follows: -- Create procedure create procedure mypro6(out sum int) begin declare num int default 0; set sum = 0; repeat-- loop starts set num = num+1; set sum = sum+num; until num>=10 end repeat; -- end of loop end; -- Call procedure call mypro6(@sum); --Query variable value select @sum; Operation Results loop statementLoop statements are used to repeatedly execute certain statements. During the execution process, you can use the The Define a stored procedure and use a loop to output the cumulative sum from 1 to 10. The code is as follows: -- Create procedure create procedure mypro7(out sum int) begin declare num int default 0; set sum = 0; loop_sum:loop-- loop starts set num = num+1; set sum = sum+num; if num>=10 then leave loop_sum; end if; end loop loop_sum; -- end of loop end; -- Call procedure call mypro7(@sum); --Query variable value select @sum; Operation Results
Stored procedure managementThe management of stored procedures mainly includes: displaying procedures, displaying procedure source code, and deleting procedures. A relatively simple way is to use the navicat client tool for management, just click the mouse, as shown in the following figure: Displaying stored procedures SHOW PROCEDURE STATUS; Displays stored procedures for a specific database SHOW PROCEDURE status where db = 'schooldb'; Displays the stored procedures of a specific schema, and requires that the stored procedures whose names contain "my" be displayed SHOW PROCEDURE status where name like '%my%'; Display the source code of the stored procedure "mypro1" SHOW CREATE PROCEDURE mypro1; Delete the stored procedure "mypro1" drop PROCEDURE mypro1; SummarizeThis concludes this article on creating, calling, and managing MySQL stored procedures. For more information about MySQL stored procedures, please search previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: JavaScript lazy loading detailed explanation
>>: A brief discussion on CSS blocking merging and other effects
Preface At work, I need to count the materials su...
Suggestion: Handwriting code as much as possible c...
1. The difference between Http and Https HTTP: It...
What is nGrinder? nGrinder is a platform for stre...
Since the default Linux kernel parameters are bas...
Compatible with new CSS3 properties In CSS3, we c...
Friends who have used the Linux system must have ...
The specific code for sending emoticons in the vu...
The complete syntax of the select statement is: S...
1. Principle of Hotlinking 1.1 Web page preparati...
This article example shares the specific code of ...
1. Unzip the downloaded file as shown below . 2. ...
Definition of Generics // Requirement 1: Generics...
1. When inserting, updating, or removing DOM elem...
MTR stands for Mini-Transaction. As the name sugg...