Previous: [MySQL Must Know and Know (19)] [Use View]
+++++++++++++Start line++++++++++++++++
Article Directory
One, the stored procedure
Need MySQL5
MySQL5 added support for stored procedures, applicable to versions after 5
Stored procedure
It is a collection of one or more MySQL statements saved for later use, which can be regarded as batch files, although their role is not limited to batch processing
Second, why use stored procedures
1. Simplify complex operations by encapsulating the processing in easy-to-use units.
2. As a series of processing steps are not required to be established repeatedly, this ensures the integrity of the data.
3. Simplifies the management of changes. The more steps, the more errors The greater the possibility
4. Improve performance, using stored procedures is faster than using separate SQL statements
5. There are some MySQL elements and features that can only be used in a single request, stored procedures can use them to write more powerful and flexible code
6. The writing of stored procedures is more complex than simple SQL statements, and writing stored procedures requires higher skills and rich experience
Three, use stored procedures
3.1 Create a stored procedure
A stored procedure that returns the average price of a product
Separator for mysql command line client
The default MySQL statement separator is;, in order to enter the following statement, the solution is to temporarily change the statement separator of the command line utility
mysql> DELIMITER //
mysql> CREATE PROCEDURE productpricing()
-> BEGIN
-> SELECT Avg(prod_price) AS priceaverage
-> FROM products;
-> END//
3.2 execute stored procedure
MySQL calls the execution of a stored procedure a call, so the statement that MySQL executes a stored procedure is CALL. CALL accepts the name of the stored procedure and any parameters that need to be passed to it.
3.3 Delete the stored procedure
Delete only if it exists
If the specified procedure does not exist, an error will be generated. DROP PROCEDURE IF EXISTS can be used when the process exists and wants to delete it
3.4 Use parameters
productpricing is just a simple stored procedure that displays the results of the SELECT statement. Generally, the stored procedure does not display the result, but returns the result to the variable you specify
variable
A specific location in memory used to temporarily store data
mysql> DELIMITER //
mysql> CREATE PROCEDURE productpricing(
-> OUT p1 DECIMAL(8,2),
-> OUT ph DECIMAL(8,2),
-> OUT pa DECIMAL(8,2)
-> )
-> BEGIN
-> SELECT Min(prod_price)
-> INTO p1
-> FROM products;
-> SELECT Max(prod_price)
-> INTO ph
-> FROM products;
-> SELECT Avg(prod_price)
-> INTO pa
-> FROM products;
-> END;
-> //
analysis
This stored procedure accepts 3 parameters, and each parameter must have a specified type, which is a decimal value.
The keyword OUT indicates that the corresponding parameter is used to pass a value from the stored procedure (return to the caller).
Keyword IN (passed to the stored procedure), INOUT (passed in and out of the stored procedure), INTO (stored to the corresponding variable).
Call the productpricing stored procedure
mysql> CALL productpricing(@pricelow,@pricehigh,@priceaverage);
-> //
variable name
All MySQL variables must start with @
Retrieve product average price
Use IN and OUT parameters
mysql> CREATE PROCEDURE ordertotal(
-> IN onumber INT,
-> OUT ototal DECIMAL(8,2)
-> )
-> BEGIN
-> SELECT Sum(item_price*quantity)
-> FROM orderitems
-> WHERE order_num = onumber
-> INTO ototal;
-> END;
-> //
analysis
onumber is defined as IN, and the order number is passed to the stored procedure. ototal is defined as OUT, and the total is returned from the stored procedure.
调用这个新存储过程
mysql> CALL ordertotal(20005, @total);
-> //
Show total
3.5 Check the stored procedure
Display the CREATE statement used to create a stored procedure
mysql> SHOW CREATE PROCEDURE ordertotal;
-> //
+++++++++++++End line++++++++++++++++