XXIII: Using Stored Procedures

@author: Tobin
@date: 2019/11/7 15:21:26

Stored procedures, a script analogy. Stored, each called directly.
The advantages of using stored procedures are simple, safe and high performance.

Simple stored procedure.

-- 执行存储过程
CALL productpricing
(
    @pricelow,
    @pricehigh,
    @priceaverage
);

-- 创建存储过程
CREATE PROCEDURE productpricing()
BEGIN
    SELECT Avg(prod_price) AS priceaverage
    FROM products;
END;

-- 调用存储过程
CALL productpricing();

-- 删除存储过程
DROP PROCEDURE productpricing;

MySQL Change line breaks DELIMITER // (you can use any symbol as a new line break, the stored procedure statement there; end is; can not explain)

With incoming and outgoing parameters stored procedure parameters.

-- 创建
CREATE PROCEDURE ordertotal
(
    IN onnumber INT;
    OUT ototal DECIMAL(8, 2)
)
BEGIN
    SELECT Sum(item_price*quantity)
    FROM orderitems
    WHERE order_num = onnumber
    INTO ototal;
END;

-- 调用
CALL ordertotal(20005, @total)
-- 显示
SELECT @total;    

IF statements can be increased

IF xxx THEN
    xxx;
END IF
-- 检查存储过程。
SHOW CREATE  PROCEDURE;
-- 显示COMMENT语句中的注释,比如存储过程是由谁在何时创建的
SHOW PROCEDURE STATUS;

Guess you like

Origin www.cnblogs.com/zuotongbin/p/11814180.html