sql过程参数

m1m5dgzv  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(402)

我试图在sql中执行一个过程,但是我在使用日期参数时遇到了一些问题

DELIMITER #
CREATE PROCEDURE conso(IN p_upcNameId VARCHAR(20), IN p_dateFrom DATE, IN p_dateTo DATE)     
BEGIN
    DECLARE done int default false;

    DECLARE v_cumule FLOAT;

    DECLARE v_reserve VARCHAR(40);
    DECLARE v_kg VARCHAR(40);
    DECLARE v_date DATETIME;

    DECLARE v_reserve_1 VARCHAR(40);
    DECLARE v_kg_1 VARCHAR(40);
    DECLARE v_date_1 DATETIME;

    DECLARE cur1 CURSOR FOR
        SELECT reserve, kg, date
        FROM
        (
            ( SELECT event_param_3 as reserve, event_param_4 as kg, event_datetime as date FROM events WHERE upcNameId = p_upcNameId AND event_code = 50 ORDER BY event_datetime DESC )
            UNION
            ( SELECT event_param_2 as reserve, event_param_3 as kg, event_datetime as date FROM events WHERE upcNameId = p_upcNameId AND event_code = 54 ORDER BY event_datetime DESC )

            UNION
            ( SELECT event_param_2 as reserve, event_param_3 as kg, event_datetime as date FROM events WHERE upcNameId = p_upcNameId AND event_code = 52 ORDER BY event_datetime DESC )
            UNION
            ( SELECT event_param_1 as reserve, event_param_2 as kg, event_datetime as date FROM events WHERE upcNameId = p_upcNameId AND event_code = 56 ORDER BY event_datetime DESC )

            UNION
            ( SELECT event_param_2 as reserve, event_param_3 as kg, event_datetime as date FROM events WHERE upcNameId = p_upcNameId AND event_code = 53 ORDER BY event_datetime DESC )
            UNION
            ( SELECT event_param_1 as reserve, event_param_2 as kg, event_datetime as date FROM events WHERE upcNameId = p_upcNameId AND event_code = 57 ORDER BY event_datetime DESC )
        ) as diffusion_programs
        ORDER BY date DESC;
    DECLARE cur2 CURSOR FOR
        SELECT reserve, kg, date
        FROM
        (
            ( SELECT event_param_3 as reserve, event_param_4 as kg, event_datetime as date FROM events WHERE upcNameId = p_upcNameId AND event_code = 50 ORDER BY event_datetime DESC )
            UNION
            ( SELECT event_param_2 as reserve, event_param_3 as kg, event_datetime as date FROM events WHERE upcNameId = p_upcNameId AND event_code = 54 ORDER BY event_datetime DESC )

            UNION
            ( SELECT event_param_2 as reserve, event_param_3 as kg, event_datetime as date FROM events WHERE upcNameId = p_upcNameId AND event_code = 52 ORDER BY event_datetime DESC )
            UNION
            ( SELECT event_param_1 as reserve, event_param_2 as kg, event_datetime as date FROM events WHERE upcNameId = p_upcNameId AND event_code = 56 ORDER BY event_datetime DESC )

            UNION
            ( SELECT event_param_2 as reserve, event_param_3 as kg, event_datetime as date FROM events WHERE upcNameId = p_upcNameId AND event_code = 53 ORDER BY event_datetime DESC )
            UNION
            ( SELECT event_param_1 as reserve, event_param_2 as kg, event_datetime as date FROM events WHERE upcNameId = p_upcNameId AND event_code = 57 ORDER BY event_datetime DESC )
        ) as diffusion_programs
        WHERE date >= p_dateFrom;
        AND date <= p_dateTo;
        ORDER BY date DESC;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
    OPEN cur1;
    OPEN cur2;

    /* Create temporary table */
    CREATE TEMPORARY TABLE tmp
    (
        date DATE,
        kg FLOAT
    );
    SET v_cumule = 0;
    FETCH cur2 INTO v_reserve_1, v_kg_1, v_date_1;

    forLoop: LOOP
        /* Actual row */
        FETCH cur1 INTO v_reserve, v_kg, v_date;

        /* Next row */
        FETCH cur2 INTO v_reserve_1, v_kg_1, v_date_1;

        /* Reserves differents TODO */
        /*IF v_reserve != v_reserve_1
        END IF;*/

        IF DATE(v_date) = DATE(v_date_1) THEN SET v_cumule = v_cumule + (v_kg_1 - v_kg);
        ELSE
            INSERT INTO tmp VALUES (DATE(v_date), v_cumule);
            SET v_cumule = 0;
        END IF;

        IF done THEN LEAVE forLoop;
        END IF;
    END LOOP;

    CLOSE cur1;
    CLOSE cur2;
END#

您可以看到p\u datefrom和p\u dateto是调用函数时要传递的参数:

DELIMITER ;
CALL conso('Name','2017-01-01','2018-01-01');
SELECT * FROM tmp;

如你所见,这是一个日期间隔。但我有个错误:
1064年-erreur de Syntax près de'和日期<=p_dateto;按日期说明订购;'声明继续处理程序'à 拉利尼53
你能帮帮我吗?

rjee0c15

rjee0c151#

“date”是mysql中的保留字。请使用其他别名。
另外,您应该考虑使用case语句来摆脱多个union语句(您可以在一个查询中生成所有union语句)。
编辑:这个错误是因为斯宾塞7593已经指出,您有多余的分号。
event_param_x列被声明为varchar列,但是您正在对这些列执行算术运算(+-)。

3hvapo4f

3hvapo4f2#

有一些假分号(语句终止符)。

WHERE date >= p_dateFrom;
       AND date <= p_dateTo;
       ORDER BY date DESC;

把那些拿走

WHERE date >= p_dateFrom
       AND date <= p_dateTo
       ORDER BY date DESC
  ;

原始(错误)答案:
这个 CONTINUE HANDLER 必须在变量声明之后,但必须在游标声明之前(我认为这在mysql参考手册中有模糊的记录。mysql存储程序非常讲究语句的顺序。)
要纠正这个错误,只需移动 DECLARE CONTINUE HANDLER 在第一个 DECLARE CURSOR .

相关问题