1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
| CREATE PROCEDURE productpricing() BEGIN SELECT Avg(prod_price) AS priceaverage FROM products; end;
-- 调用存储过程 CALL productpricing();
-- 删除存储过程 DROP PROCEDURE productpricing;
-- 使用参数 CREATE PROCEDURE productpricing( OUT pl DECIMAL(8, 2), OUT ph DECIMAL(8, 2), OUT pa DECIMAL(8, 2) ) BEGIN SELECT Min(prod_price) INTO pl FROM products; SELECT Max(prod_price) INTO ph FROM products; SELECT Avg(prod_price) INTO pa FROM products; END;
-- 调用,所有MySQL变量名都必须以 @ 开始 CALL productpricing(@pricelow, @pricehigh, @priceaverage);
-- 显示 SELECT @pricelow, @pricehigh, @priceaverage;
-- IN、OUT 参数 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;
-- 调用 CALL ordertotal(20005, @total);
|