标签: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 variableCREATE PROCEDURE ordertotal(IN onumber INT,IN taxable BOOLEAN,OUT ototal DECIMAL(8, 2)) COMMENT ‘Obtain order total, optionally adding tax‘BEGIN--Declare variable for totalDECLARE total DECIMAL(8, 2);--Declare tax percentageDECLARE taxrate INT DEFAULT 6;--GET the order totalSELECT Sum(item_price*quantity)FROM orderitemsWHERE order_num = onumberINTO total;--Is this taxableIF taxable THENSELECT 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 totalDECLARE total DECIMAL(8, 2);--Declare tax percentageDECLARE taxrate INT DEFAULT 6;--GET the order totalSELECT Sum(item_price*quantity)FROM orderitemsWHERE order_num = onumberINTO total;
--Is this taxableIF taxable THENSELECT 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()BEGINDECLARE ordernumbers CURSORFORSELECT order_num FROM orders;END;
CREATE PROCEDURE processorders()BEGIN--DeclareDECLARE ordernumbers CURSORFORSELECT order_num FROM orders;--OpenOPEN ordernumbers;--CloseCLOSE ordernumbers;END;
CREATE PROCEDURE processorders()BEGIN--DeclareDECLARE o INT;DECLARE ordernumbers CURSORFORSELECT order_num FROM orders;--OpenOPEN ordernumbers;--GetFETCH ordernumbers INTO o;--CloseCLOSE ordernumbers;END;
CREATE PROCEDURE processorders()BEGIN--Declare local variablesDELCARE done BOOLEAN DEFAULT 0;DECLARE o INT;DECLARE t DECIMAL(8,2);--Declare cursorDECLARE ordernumbers CURSORFORSELECT order_num FROM orders;--Declare continue handlerDECLARE CONTINUE HANDLER FOR SQLSTATE ‘02000‘ SET done=1;--Create a table to store the resultsCREATE TABLE IF NOT EXISTS ordertotals(order_num INT, total DECIMAL(8,2));--Open the cursorOPEN ordernumbers;--Loop through all rowsREPEATFETCH ordernumbers INTO o;CALL ordertotal(o, 1, t);INSERT INTO ordertotals(order_num, total) VALUES(o, t);UNTIL done END REPEAT;--Close the cursorCLOSE 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