MySQL must know must know the study notes Chapter 23 using stored procedures

Support for stored procedures has been added in MySQL 5.

Sometimes an operation requires multiple SQL statements to complete. You can create a stored procedure, which stores one or more SQL statements.

Reasons for using stored procedures:
1. Simplify complex operations by encapsulating processing in easy-to-use units.
2. Everyone uses the same stored procedure to replace a complex operation, which can prevent errors, ensure that everyone uses the same code, and ensure data consistency.
3. Simplify the management of changes. If you need to change the table name, column name, etc., you only need to change it in the storage process, and the person using it does not even need to know these changes.
4. Improve performance, using stored procedures is faster than using separate SQL.
5. There are some features that can only be used in a single request, stored procedures can be used to write stronger and more flexible code.

Defects of stored procedure:
1. Writing is more complicated than SQL.
2. There may not be security access to create a stored procedure. Many DBAs restrict the creation of stored procedures, but allow users to use stored procedures.

Execute the stored procedure:

CALL procedureName(parameterList);

Create a stored procedure:

CREATE PROCEDURE procedureName()
BEGIN
    存储过程内容
END;

When using the command line client, the default MySQL statement delimiter is ;. When creating a stored procedure on the command line, the delimiter in the ;stored procedure will become the terminator created by the stored procedure, resulting in a syntax error. The solution is to temporarily change the statement separator:

DELIMITER //    # 将分隔符改为//

CREATE PROCEDURE procedureName()
BEGIN
    存储过程内容
END//

DELIMITER ;

Except \, any character can be used as a statement separator.

Delete the stored procedure:

DROP PROCEDURE procedureName;

If this stored procedure does not exist, an error will be reported, if you do not want to report an error:

DROP PROCEDURE IF EXISTS procedureName;

The following is the sales information table:
Insert picture description here
Create a stored procedure to obtain the lowest, highest, and average prices:

CREATE PROCEDURE productpricing(
    OUT pl DECIMAL(8,2),
    OUT ph DECIMAL(8,2),
    OUT pa DECIMAL(8,2)
)
BEGIN
    SELECT MIN(amount), MAX(amount), AVG(amount)
    INTO pl, ph, pa    # 将三个聚集函数的结果插入参数
    FROM payment;
END;

MySQL supports three types of parameters: IN (passed to the stored procedure), OUT (passed from the stored procedure), and INOUT (passed into and out of the stored procedure).

The allowed data types of the parameters of the stored procedure are the same as those used in the table. The return type is not allowed to be a record set, so multiple rows and columns cannot be returned with one parameter.

Call the above stored procedure:
Insert picture description here
Variables in MySQL start with @.

Get variables:
Insert picture description here
complex stored procedures:

-- NAME: ordertotal
-- Patameters: onumber = order number
--             taxable = 0 if not taxable, 1 if taxable
--             ototal  = order total variable

CREATE PROCEDURE ordertotal(
    IN onumber INT,
    IN taxable BOOLEAN,
    OUT ototal DECIMAL(8,2)
) COMMENT 'Obtain order total, optionally adding tax'
BEGIN

    -- Declare variable for total
    DECLARE total DECIMAL(8,2);
    -- Declare tax pricentage
    DECLARE taxrate INT DEFAULT 6;
    
    -- Get the order total
    SELECT Sum(item_price * quantity)
    FROM orderitems
    WHERE order_num = onumber
    INTO total;

    -- Is this taxable?
    IF taxable THEN
        -- Yes, so add taxrate to the total
        SELECT total + (total / 100 * taxrate) INTO total;
    END IF;

    -- And finally, save to our variable
    SELECT total INTO ototal;
END;

In the above example, --a comment beginning with (a space after it) is added. MySQL supports three types of comments, and also supports #and /*...*/.

The above example also uses the DECLARE statement to define two local variables, requiring the variable name and data type to be specified, and it supports optional default values.

The IF in the above example checks whether taxable is true, and also supports ELSEIF (using the THEN clause) and ELSE (not using the THEN clause) clauses.

The COMMENT keyword can be SHOW PROCEDURE STATUSdisplayed in. This command can also display a list of stored procedures with detailed information such as when and who created it. The following SQL can be used to limit the output:

SHOW PROCEDURE STATUS LIKE 'pattern';

Display the creation statement of the stored procedure:

SHOW CREATE PROCEDURE procedureName;

Guess you like

Origin blog.csdn.net/tus00000/article/details/111563804