Delimiter//
In general, MySQL ends with a confirmation input and executes a statement, but in a stored procedure ; It is not the end, so you can use the command to change the number to // to confirm the input and execute.
I. Creating a stored Procedure
1. Basic syntax:
CREATE PROCEDURE Sp_name ()
Begin
.........
End2. Parameter passing
Two. Calling a stored procedure
1. Basic syntax: Call Sp_name ()
Note: The stored procedure name must be appended with parentheses, even if the stored procedure has no parameters passed
Three. Delete a stored procedure
1. Basic syntax:
drop procedure sp_name//
2. Precautions
(1) cannot delete another stored procedure in one stored procedure, only another stored procedure can be called
Four. blocks, conditions, loops
1. Block definition, commonly used
Begin
......
End
You can also alias chunks, such as:
Lable:begin
...........
End lable;
You can use leave lable, jump out of chunks, execute code after chunk
2. Conditional statements
if condition Then
Statement
Else
Statement
end if;
3. Looping statements
(1). While loop
[Label:]while expression do
Statements
END While [label] ;
(2). Loop loop
[Label:]LOOP
Statements
END LOOP [label]; (3). Repeat until cycle
[Label:]REPEAT
Statements
UNTIL expression
END REPEAT [label] ;
Five. Other common commands
1.show Procedure Status
Displays basic information about all stored procedures in the database, including the owning database, stored procedure name, creation time, etc.
2.show CREATE PROCEDURE Sp_name
Show details of a stored procedurelet's look at an example
One, MySQL create stored procedure
"Pr_add" is a simple MySQL stored procedure, this MySQL stored procedure has two int type input parameter "a", "B", return these two parameter's and
Delimiter//--Change the separator
drop procedure if exists pr_add//--delete if the stored procedure was previously created
Calculate the sum of two numbers
- CREATE PROCEDURE Pr_add (a int,bint)
- Begin
- declare CInt;
- if A is null then
- Set a =0;
- End If;
- if b is null and then
- Set b = 0;
- endif
- Set c = a +b;
- Select C as Sum;
- End
- //
Second, call the MySQL stored procedure
- Call Pr_add (10, 20);
Executes the mysql stored procedure with the stored procedure parameter as a MySQL user variable.
- SET @A =ten;
- SET @b = +;
- Call Pr_add (@a, @b);
Third, the MySQL stored procedure characteristic
The simple syntax for creating a MySQL stored procedure is:
- Create procedure stored procedure name ()
- (
- [In|out|inout] Parameter datatype
- )
- Begin
- MySQL statement;
- End
MySQL stored procedure Parameters If you do not explicitly specify "in", "Out", and "InOut", the default is "in". In practice, we do not explicitly specify parameters that are "in".
1. The "()" after the name of the MySQL stored procedure is required, even if there is no parameter, "()"
2. MySQL stored procedure parameter, cannot add "@" before parameter name, for example: "@a int". The following create stored procedure syntax is wrong in MySQL (correct in SQL Server). The variables in the MySQL stored procedure do not need to be "@" before the variable name, although the MySQL client user variable should be added "@".
- Create Procedurepr_add
- (
- @a int,--Error
- b INT--Correct
- )
3. The parameters of the MySQL stored procedure cannot specify a default value.
4. The MySQL stored procedure does not need to precede the procedure body with "as". The SQL Server stored procedure must be added with the "as" keyword.
- Create Procedurepr_add
- (
- a int,
- b int
- )
- As--error, MySQL does not need "as"
- Begin
- MySQL statement ...;
- End
5. If the MySQL stored procedure contains more than one MySQL statement, the BEGIN END keyword is required.
- Create Procedurepr_add
- (
- a int,
- b int
- )
- Begin
- MySQL statement 1 ...;
- MySQL statement 2 ...;
- End
6. At the end of each statement in the MySQL stored procedure, add a semicolon ";"
- ...
- declare c int;
- If A is null then
- Set a = 0;
- End If;
- ...
- End
7. Comments in the MySQL stored procedure.
- declare c int; --This is a single-line MySQL note (note--there must be at least one space)
- If a is null and then # This is also a single-line MySQL comment
- Set a =0;
- End If;
- ...
- End
8. The "return" keyword cannot be used in a MySQL stored procedure.
- Set c =a+ b;
- Select C as Sum;
- End
9. When calling the MySQL stored procedure, you need to add "()" After the procedure name, even if there is no parameter, "()"
- Call Pr_no_param ();
10. Because the MySQL stored procedure parameter does not have a default value, the parameter cannot be omitted when the MySQL stored procedure is called. can be substituted with null.
Mysql stored Procedure Basic syntax