第二十三章:使用存储过程

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

存储过程,类比一个脚本。存储起来,每次直接调用。
使用存储过程的优势是简单,安全,高性能。

简单的存储过程。

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

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

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

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

MySQL更改换行符DELIMITER //(可以使用任意符号作为新的换行符,存储过程语句中有;结束也是;解释不了)

带传入参数和传出参数的存储过程。

-- 创建
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语句

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

猜你喜欢

转载自www.cnblogs.com/zuotongbin/p/11814180.html