标签:
call productpricing ( @ pricelow,
@ pricehigh,
@ priceaverage
);
CREATE PROCEDURE productpricing() BEGIN SELECT AVG(prod_price) AS priceaverage FROM products; END;
DELIMITER // CREATE PROCEDURE productpricing() BEGIN SELECT AVG(prod_price) AS priceaverage FROM products; END // DELIMITER ;
CALL productpricing();
结果是:
+--------------+ | priceaverage | +--------------+ | 16.133571 | +--------------+
DROP PROCEDURE productpricing;
DELIMITER // CREATE PROCEDURE pricing( OUT pl DECIMAL(8, 2), OUT ph DECIMAL (8, 2), OUT pa DECIMAL (8, 2), ) BEGIN SELECT MIN (prod_price) INTO pl FROM productes; SELECT MAX (prod_price) INTO ph FROM productes; SELECT AVG (prod_price) INTO pa FROM productes; END // DELIMITER ;
CALL pricing (@pricelow, @pricehigh, @pricevarage );
SELECT @pricevarage
输出:
+--------+ | @pricevarage| +--------+ | 55.00 | +--------+
为了获得 3个值,可使用以下语句:
SELECT @pricelow, @pricehigh, @pricevarage ;
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);
SELECT @total;
输出:
+--------+ | @total | +--------+ | 192.37| +--------+
call ordertotal(20009, @total); SELECT @total;
输出:
+--------+ | @total | +--------+ | 38.47 | +--------+
以后我们每次要通过订单号,来获得商品的总价都可以使用这个方式。是不是很有用啊。。
-- Name: ordertotal // 添加注释
-- Parameters: onumber = order number
-- taxable = 0 if not taxable, 1 if taxtable
-- 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 percentage
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 out variable
SELECT total INTO ototal;
END;
call ordertotal(20009, 0,@total); SELECT @total;
输出:
+--------+ | @total | +--------+ | 38.47 | +--------+
第二条:
call ordertotal(20009, 1,@total); SELECT @total;
输出:
+--------+ | @total | +--------+ | 36.21 | +--------+
show create procedure ordertotal;
SHOW PROCEDURE STATUS like ‘ordertotal‘;
标签:
原文地址:http://www.cnblogs.com/myc618/p/4650494.html