在过程内创建临时表

时间:2015-11-26 11:11:05

标签: mysql database stored-procedures procedure

以下是我尝试运行以下代码时遇到的错误。

我在mySQL教程+论坛上花了一天没有成功。

enter image description here

DROP PROCEDURE IF EXISTS update_end_date_dwh_dimension_product;

delimiter $$
CREATE PROCEDURE update_end_date_dwh_dimension_product()
BEGIN

DROP TEMPORARY TABLE IF EXISTS __temp_BI__;

CREATE TEMPORARY TABLE __temp_BI__
SELECT dp.sku, dp.start_date
FROM  dwh_dimension_product dp
ORDER BY dp.sku, dp.start_date;

DECLARE end_loop BOOLEAN DEFAULT TRUE;

DECLARE sku1_ VARCHAR(50);
DECLARE sku2_ VARCHAR(50);   

DECLARE start_date1_ TIMESTAMP;
DECLARE start_date2_ TIMESTAMP;

DECLARE cursor_temp CURSOR
    FOR select * from __temp_BI__;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET end_loop = FALSE; 

OPEN cursor_temp;

FETCH cursor_temp INTO sku1_, start_date1_;

while_label: WHILE (end_loop) DO

    IF end_loop = FALSE THEN
        LEAVE while_label;
    END IF;

    FETCH cursor_temp INTO sku2_, start_date2_;

    while_label2: WHILE (sku1_ = sku2_) DO
        UPDATE dwh_dimension_product dp1
        SET dp1.end_date = DATE_SUB(start_date2_, INTERVAL 1 SECOND)
        WHERE  dp1.sku = sku1_
        AND dp1.start_date = start_date1_;

        SET sku1_ = sku2_;
        SET start_date1_ = start_date2_;
        FETCH cursor_temp INTO sku2_, start_date2_;

        IF end_loop = FALSE THEN
            LEAVE while_label2;
        END IF;
    END WHILE;

    UPDATE dwh_dimension_product dp2
    SET dp2.end_date = NULL
    WHERE dp2.sku = sku1_
    AND dp2.start_date = start_date1_;

    SET sku1_ = sku2_;
    SET start_date1_ = start_date2_;
END WHILE;
CLOSE cursor_temp;
DROP TABLE IF EXISTS __temp_BI__;
END $$  
delimiter ;

如果我将以下代码放在程序()之外,它可以正常工作

DROP TEMPORARY TABLE IF EXISTS __temp_BI__;

CREATE TEMPORARY TABLE __temp_BI__
SELECT dp.sku, dp.start_date
FROM  dwh_dimension_product dp
ORDER BY dp.sku, dp.start_date;

感谢您的建议。

0 个答案:

没有答案