The MySQL stored procedure is a new feature added since MySQL 5.0.
The advantages of stored procedures are as follows. However, the most important thing is the execution efficiency and SQL code encapsulation.
In particular, the SQL code encapsulation function. If there is no stored procedure, many SQL statements need to be organized when external programs access the database (such as PHP. Especially when the business logic is complex, a lot of SQL statements and conditions are mixed in PHP code, which is chilling. With the MySQL stored procedure, the business logic can encapsulate the stored procedure, which is not only easy to maintain, but also highly efficient in execution.
I. MySQL Stored Procedure
"Pr_add" is a simple MySQL stored procedure, which has two int-type input parameters: "a" and "B", and returns the sum of the two parameters.
c a a b b c a c ;
Ii. Call the MySQL Stored Procedure
call pr_add(, );
Execute the MySQL stored procedure. The stored procedure parameter is a MySQL user variable.
, );
Iii. MySQL stored procedure features
The simple syntax for creating a MySQL stored procedure is:
;
1. The "()" after the MySQL stored procedure name is required. Even if there is no parameter, "()" is required.
2. For MySQL stored procedure parameters, you cannot add "@" before the parameter name, for example, int ". The following syntax for creating a stored procedure is incorrect in MySQL (correct in SQL Server ). You do not need to add "@" before the variable name in the MySQL stored procedure, although the MySQL client user variable requires "@".
, b )
4. You do not need to add "as" before procedure body to the MySQL stored procedure ". The SQL Server Stored Procedure must contain the "as" keyword.
. If the MySQL Stored Procedure contains multiple MySQL statements, you need;
c a a
c ; a a ;
c a c ;
call pr_no_param();
call pr_add(, );