Home >Database >Mysql Tutorial >mysql procedure function

mysql procedure function

PHPz
PHPzOriginal
2023-05-08 13:01:38916browse

The procedures and functions in MySQL can be used to encapsulate a series of SQL statements to implement complex business logic. This article will introduce the procedures and functions in MySQL, and explain them in terms of syntax, execution flow, and sample programs.

1. Procedure in MySQL

A procedure is a mechanism in MySQL that encapsulates a series of SQL statements, similar to functions in C language. MySQL procedures can receive input parameters, return output parameters and return result sets, so they are very suitable for encapsulating complex business logic.

1. Syntax

The syntax for creating a process is as follows:

CREATE PROCEDURE procedure_name ([IN | OUT | INOUT] parameter_name parameter_type)
BEGIN
   statements;
END;

Among them, procedure_name is the name of the process, parameter_name is the name of the parameter passed in or returned by the process, and parameter_type is the parameter The type can be any standard MySQL data type.

2. Execution process

The execution process flow is as follows:

  • The client initiates a request to call the process;
  • MySQL server obtains the request, Parse the name and parameters of the process;
  • The code block of the MySQL execution process and return the results to the client.

3. Sample program

The following is a sample program, including input parameters and output parameters:

CREATE PROCEDURE sum(IN a INT, IN b INT, OUT c INT)
BEGIN
   SET c = a + b;
END;

After executing the above program and creating it successfully, you can use the following Method to call this process:

CALL sum(1, 2, @result);
SELECT @result;

The meaning of the above code is: call the sum process, pass in parameters 1 and 2, store the return value in the user variable @result, and display the value of @result through the SELECT statement.

2. Functions in MySQL

Function is another mechanism in MySQL that encapsulates a series of SQL statements, and is similar to the function in C language. Compared with procedures, functions are more lightweight. They do not support output parameters and result sets, but only input parameters and return values.

1. Syntax

The syntax for creating a function is as follows:

CREATE FUNCTION function_name ([parameter_name parameter_type])
RETURNS return_type
BEGIN
   statements;
   RETURN return_value;
END;

Among them, function_name is the function name, parameter_name is the input parameter name of the function, parameter_type is the type of the parameter, you can is any standard MySQL data type, return_type is the type of the function return value.

2. Execution process

The process of executing a function is similar to the execution process. The client initiates a call request, the MySQL server parses the request and executes the code block of the function, and finally returns the result to the client. end.

3. Sample program

The following is a sample program, including input parameters and return values:

CREATE FUNCTION cube(x INT)
RETURNS INT
BEGIN
   RETURN x * x * x;
END;

After the above program is successfully created, it can be called in the following ways Function:

SELECT cube(2);

The meaning of the above code is: call the cube function, pass in parameter 2, and return the result 8.

3. Summary

When developing large-scale MySQL applications, procedures and functions can help us better organize SQL statement codes and improve development efficiency. The difference between the two is that the process supports input parameters, output parameters and result sets, and is suitable for complex business logic encapsulation; while the function only supports input parameters and return values, and is suitable for simple mathematical or logical calculations. Whether it is a procedure or a function, you can learn and master it through syntax, execution flow and sample programs.

The above is the detailed content of mysql procedure function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:mysql in query stringNext article:mysql in query string