gpt4 book ai didi

mysql - MySQL SP 中的更新语句显示缺少结束错误

转载 作者:行者123 更新时间:2023-11-29 05:09:56 25 4
gpt4 key购买 nike

我有一个如下所示的存储过程。游标声明之前的更新语句显示错误。当我删除游标语句时,存储过程工作正常。为什么游标语句之前不能有update语句?

DROP PROCEDURE IF EXISTS GenerateAlert;
DELIMITER $$
CREATE PROCEDURE GenerateAlert()
BEGIN
DECLARE done INTEGER DEFAULT FALSE;
DECLARE temp_project_id INT DEFAULT 0;
DECLARE temp_finish_time DATETIME DEFAULT NOW();
DECLARE _message_class INT DEFAULT 3;
DECLARE proj_user_id INT DEFAULT 0;


-- get message text template
DECLARE message_template VARCHAR(255) DEFAULT
(
SELECT alert_message_template FROM alerts WHERE id = 6
);



-- get maximum allowed minutes
DECLARE allowed_time INT DEFAULT
(
SELECT alert_value_1 FROM alerts WHERE id = 4
);

-- flag all messages with class 3 to deleted = true
UPDATE messages
SET is_deleted = 1
WHERE messages_class = 3;

DECLARE _cur CURSOR FOR
SELECT d.project_id, MAX(finish_time)
FROM
client_docs AS d INNER JOIN
issues AS i ON d.project_id = i.project_id INNER JOIN
working_times AS t ON i.id = t.issue_id
WHERE
d.status = 1 AND
t.finish_time IS NOT NULL AND
t.category = 1
GROUP BY d.project_id;

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN _cur;
read_loop: LOOP

FETCH _cur INTO temp_project_id, temp_finish_time;

IF done THEN
LEAVE read_loop;
END IF;

-- add more minutes to finish time
SET temp_finish_time = DATE_ADD(temp_finish_time, INTERVAL allowed_time MINUTE);

IF temp_finish_time < NOW() THEN

-- this project must be alerted

-- get project user
SELECT c.staff INTO proj_user_id
FROM
projects AS p INNER JOIN
clients AS c ON p.client_id = c.id
WHERE
p.id = temp_project_id;





END IF;

END LOOP read_loop;
CLOSE _cur;


END
$$
DELIMITER ;

最佳答案

找到答案:声明语句必须在 MySQL 存储过程中的任何其他事务语句之前。 SQL, missing end, but why?答案中的第二条评论。

关于mysql - MySQL SP 中的更新语句显示缺少结束错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41155683/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com