开发者

How to debug a syntax error in my MySQL cursor code?

开发者 https://www.devze.com 2023-02-28 02:25 出处:网络
I have an error with this cursor code: delimiter// CREATE FUNCTION updateOrder() DETERMINISTIC BEGIN DECLARE done INT DEFAULT 0;

I have an error with this cursor code:

delimiter//
CREATE FUNCTION updateOrder()
DETERMINISTIC
BEGIN
DECLARE done INT DEFAULT 0;
# déclare les variables qui vont accueillir les données retournées par la requête
DECLARE i_id_produit INTEGER;
DECLARE old_id_produit INTEGER;
DECLARE cpt INTEGER;
# déclare le curseur
DECLARE ccustomers CURSOR FOR SELECT id_produit
FROM produit_documentation order by id_produit;
# déclare un handler pour détecter la fin du jeu d'enregistrements
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
# ouvr开发者_运维知识库e le curseur
OPEN ccustomers;
REPEAT
FETCH ccustomers INTO i_id_produit;
IF i_id_produit = old_customer_id THEN
UPDATE produit_documentation SET ordre = cpt;
cpt++;
ELSE old_customer_id = i_id_produit;
END IF;
UNTIL done END REPEAT;
END
//

I'm getting this error:

Script line: 2  You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 
'DETERMINISTIC
BEGIN
DECLARE done INT DEFAULT 0;
# déclare les variables qui ' at line 2

I use MySQL 5.5.


You need a RETURNS clause after your CREATE FUNCTION, like this:

DELIMITER //
CREATE FUNCTION updateOrder() RETURNS varchar(255)
DETERMINISTIC
...

See this for details.

Don't forget to add a matching RETURN statement and to set your delimiter back to ; at the end. :)

ETA: Or make it a PROCEDURE instead, as in this example.

0

精彩评论

暂无评论...
验证码 换一张
取 消