MySQL creates a stored procedure (CREATE PROCEDURE)

MySQL stored procedure is a collection of some SQL statements. For example, sometimes we may need a large series of SQL statements, or we need to set some variable values ​​in the process of writing SQL statements. At this time, it is absolutely necessary for us to write a stored procedure.

Writing stored procedures is not a simple matter, but using stored procedures can simplify operations and reduce redundant operation steps. At the same time, it can also reduce errors during operations and improve efficiency. Therefore, you should learn to use stored procedures as much as possible.

The following mainly introduces how to create a stored procedure.

You can use the CREATE PROCEDURE statement to create a stored procedure, the syntax is as follows:
CREATE PROCEDURE <过程名> ( [过程参数[,]] ) <过程体>
[过程参数[,]] 格式
[ IN | OUT | INOUT ] <参数名> <类型>

The syntax is explained as follows:

1) Procedure name

The name of the stored procedure, which is created in the current database by default. If you need to create a stored procedure in a specific database, add the name of the database before the name, ie db_name.sp_name.

It should be noted that the name should try to avoid choosing the same name as the MySQL built-in function, otherwise an error will occur.

2) Process parameters

A list of parameters for the stored procedure. Where, <参数名> is the parameter name, <类型> and is the parameter type (it can be any valid MySQL data type). When there are multiple parameters, separate them with commas in the parameter list. A stored procedure can have no parameters (the name of the stored procedure still needs to be followed by a pair of parentheses), or it can have one or more parameters.

MySQL stored procedures support three types of parameters, namely input parameters, output parameters and input/output parameters, which are identified by the three keywords IN, OUT and INOUT respectively. Among them, the input parameter can be passed to a stored procedure, the output parameter is used in the situation that the stored procedure needs to return an operation result, and the input/output parameter can be used as both an input parameter and an output parameter.

It should be noted that the name of the parameter should not be the same as the column name of the data table, otherwise, although no error message will be returned, the SQL statement of the stored procedure will regard the parameter name as the column name, resulting in unpredictable results.

3) Process body

The main part of a stored procedure, also known as the stored procedure body, contains the SQL statements that must be executed when the procedure is called. This section begins with the keyword BEGIN and ends with the keyword END . If there is only one SQL statement in the stored procedure body, the BEGIN-END flag can be omitted.

In the creation of stored procedures, a very important MySQL command is often used, that is, the DELIMITER command, especially for users who operate the MySQL database through the command line, they must learn to use this command.

In MySQL, the server processes SQL statements with a semicolon as the end of the statement by default. However, when creating a stored procedure, the stored procedure body may contain multiple SQL statements. If these SQL statements still use a semicolon as the statement terminator, the MySQL server will end with the first SQL statement encountered during processing. The semicolon is used as the terminator of the entire program, instead of processing the SQL statement behind the stored procedure body, which obviously does not work.

To solve the above problems, usually use the DELIMITER command to modify the end command to other characters. The syntax format is as follows:
DELIMITER $$

The syntax is explained as follows:

  • $$ is a user-defined terminator, usually this symbol can be some special symbols, such as two "?" or two "¥" and so on.
  • When using the DELIMITER command, the backslash "\" character should be avoided because it is a MySQL escape character.

Enter the following SQL statement on the MySQL command line client.
mysql> DELIMITER ??

After this SQL statement is successfully executed, the end mark of any command, statement or program is replaced by two question marks "??".



If you want to switch back to the default semicolon ";" as the end mark, you can enter the following statement on the MySQL command line client:

mysql> DELIMITER ;

Note: There must be a space between DELIMITER and the semicolon ";" . When creating a stored procedure, you must have CREATE ROUTINE permission.

Example 1

Create a stored procedure named ShowStuScore. The function of the stored procedure is to query the student's grade information from the student grade information table. The entered SQL statement and execution process are as follows.
mysql> DELIMITER //
mysql> CREATE PROCEDURE ShowStuScore()
    -> BEGIN
    -> SELECT * FROM tb_students_score;
    -> END //
Query OK, 0 rows affected (0.09 sec)

The result shows that the ShowStuScore stored procedure has been created successfully.

Example 2

Create a stored procedure named GetScoreByStu with the input parameter being the student name. The function of the stored procedure is to query the grade information of the specified student from the student grade information table through the entered student name. The entered SQL statement and execution process are as follows.
mysql> DELIMITER //
mysql> CREATE PROCEDURE GetScoreByStu
    -> (IN name VARCHAR(30))
    -> BEGIN
    -> SELECT student_score FROM tb_students_score
    -> WHERE student_name=name;
    -> END //
Query OK, 0 rows affected (0.01 sec)

Guess you like

Origin blog.csdn.net/weixin_56175092/article/details/130370485