标签:percent get handle 类型 val 1.5 number 例子 pen
--创建存储过程
CREATE PROCEDURE <存储过程的名称>(<变量的类型定义>)
BEGIN
<执行操作>
END;
--执行存储过程
CALL <存储过程的名称>(<@变量名>);
--删除存储过程
DROP PROCEDURE <存储过程的名称>;
-- Name: ordertotal
-- Parameters: 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 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
SELECT total+(total/100*taxrate) INTO total;
END IF;
SELECT total INTO ototal;
- END;
CREATE PROCEDURE ordertotal(
IN onumber INT,
IN taxable BOOLEAN,
OUT ototal DECIMAL(8, 2)
) COMMENT ‘Obtain order total, optionally adding tax‘
BEGIN
...
END;
--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
SELECT total+(total/100*taxrate) INTO total;
END IF;
public void ordertotal(int onumber, boolean taxable, double ototal) {
double total;
int taxrate = 6;
total = getOrderTotal(onumber);
if (taxable) {
total += total / (100 * taxrate);
}
ototal = total;
}
--不含营业税
CALL ordertotal(20005, 0, @total);
SELECT @total
+----------+
| @total |
+----------+
| 149.87 |
+----------+
--包含营业税
CALL ordertotal(20005, 1, @total);
SELECT @total
+-----------------+
| @total |
+-----------------+
| 158.862200000 |
+-----------------+
SHOW CREATE PROCEDURE ordertotal;
DROP PROCEDURE ordertotal;
CREATE PROCEDURE processorders()
BEGIN
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
END;
CREATE PROCEDURE processorders()
BEGIN
--Declare
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
--Open
OPEN ordernumbers;
--Close
CLOSE ordernumbers;
END;
CREATE PROCEDURE processorders()
BEGIN
--Declare
DECLARE o INT;
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
--Open
OPEN ordernumbers;
--Get
FETCH ordernumbers INTO o;
--Close
CLOSE ordernumbers;
END;
CREATE PROCEDURE processorders()
BEGIN
--Declare local variables
DELCARE done BOOLEAN DEFAULT 0;
DECLARE o INT;
DECLARE t DECIMAL(8,2);
--Declare cursor
DECLARE ordernumbers CURSOR
FOR
SELECT order_num FROM orders;
--Declare continue handler
DECLARE CONTINUE HANDLER FOR SQLSTATE ‘02000‘ SET done=1;
--Create a table to store the results
CREATE TABLE IF NOT EXISTS ordertotals(order_num INT, total DECIMAL(8,2));
--Open the cursor
OPEN ordernumbers;
--Loop through all rows
REPEAT
FETCH ordernumbers INTO o;
CALL ordertotal(o, 1, t);
INSERT INTO ordertotals(order_num, total) VALUES(o, t);
UNTIL done END REPEAT;
--Close the cursor
CLOSE ordernumbers;
END;
SELECT * FROM ordertotals;
+---------+---------+
| 20005 | 158.86 |
| 20006 | 58.30 |
| 20007 | 1060.00 |
| 20008 | 132.50 |
| 20009 | 40.78 |
+---------+---------+
标签:percent get handle 类型 val 1.5 number 例子 pen
原文地址:http://www.cnblogs.com/deng-cc/p/7986087.html