带游标的相互调用的存储过程

来源:互联网 发布:淘宝网苹果电脑版下载 编辑:程序博客网 时间:2024/05/17 03:58

--------------------子存储过程-------------------

DELIMITER //



CREATE

    PROCEDURE `proc_hanshui`(
    IN onumber VARCHAR(40),
    IN taxable BOOLEAN,
    OUT ototal DECIMAL(18,2)
    )

    BEGIN
    DECLARE total DECIMAL(18,2);
    DECLARE taxrate INT DEFAULT 6;
    
    SELECT SUM(totalprice*adult)
    FROM ht_order
    WHERE orderid = onumber
    INTO total;
    
    IF taxable THEN
        SELECT total+(total/100*taxrate) INTO total;
    END IF;
    
    SELECT total INTO ototal;

   END//
DELIMITER ;


--------------------------------父存储过程-------------------------------------------------------------

DELIMITER //


CREATE

    PROCEDURE `proc_meiyue_hanshui`( )

    BEGIN
    -- declare local variables
    DECLARE done BOOLEAN DEFAULT 0;
    DECLARE o VARCHAR(40);
    
    DECLARE t DECIMAL(18,2);
    
    -- declare the cursor
    DECLARE orderids CURSOR
    FOR
    -- SELECT order_number FROM orders;
    SELECT orderid FROM ht_order WHERE orderid LIKE "HT201303%";
    -- declare continue handler
    DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done=1;
    
    -- create a table to store the results
    DROP TABLE IF EXISTS ordertotals;
    CREATE TABLE IF NOT EXISTS ordertotals
        (order_num VARCHAR(40),total DECIMAL(18,2));
        
    -- open the cursor
    OPEN orderids;
    
    -- loop through all rows
    REPEAT
    
        -- get order number
        FETCH orderids INTO o;
        
        -- get the total for this order
        CALL proc_hanshui(o, 1, t);  -- 执行另一个获取收入的存储过程
        
        -- insert order and total into ordertotals
        INSERT INTO ordertotals(order_num, total)
             VALUES(o, t);
        SELECT o,t;
        
    -- end of LOOP
    UNTIL done END REPEAT;
    
    -- close the cursor
    CLOSE orderids;

   END//

DELIMITER ;

---------------------------------------------------------------------------------------------

0 0
原创粉丝点击