gpt4 book ai didi

mysql - 我的 MySQL 脚本语法错误

转载 作者:行者123 更新时间:2023-11-29 00:30:03 25 4
gpt4 key购买 nike

我正在尝试创建一个存储函数,但出现了一个让我发疯的错误,我真的不知道我的脚本有什么问题,我认为它没有错误,但我不知道知道为什么我会收到此错误:

1064 - 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 'DECLARE cr1 CURSOR FOR SELECT codeArt, qte FROM LigneBonEntrée WHERE numBon = '

这是我的脚本:

DELIMITER $$

CREATE DEFINER=root@localhost FUNCTION verifierQteDemandee(numBonIn INT) RETURNS BOOLEAN
BEGIN
DECLARE numLignesBonEntrée, numLignesBonSortie INTEGER;
DECLARE codeArtLigneBonEntrée, codeArtLigneBonSortie, qteLigneBonEntrée, qteLigneBonSortie INTEGER;
DECLARE no_more_rows BOOLEAN;
DECLARE qteArticle INTEGER;
DECLARE test BOOLEAN DEFAULT TRUE;

SET numLignesBonEntrée = (SELECT COUNT(*) FROM LigneBonEntrée WHERE numBon = numBonIn);

SET numLignesBonSortie = (SELECT COUNT(*) FROM numLignesBonSortie WHERE numBon = (SELECT estLieA FROM LigneBonEntrée WHERE numBon = numBonIn));

IF numLignesBonEntrée <> numLignesBonSortie THEN
SET test = FALSE;
ELSE
DECLARE cr1 CURSOR FOR SELECT codeArt, qte FROM LigneBonEntrée WHERE numBon = numBonIn ORDER BY codeArt ASC;

DECLARE cr2 CURSOR FOR SELECT codeArt, qte FROM LigneBonSortieWHERE numBon = (SELECT estLieA FROM LigneBonEntréeWHERE numBon = numBonIn) ORDER BY codeArt ASC;

DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_rows = TRUE;

OPEN cr1;
OPEN cr2;

the_loop: LOOP
FETCH cr1 INTO codeArtLigneBonEntrée, qteLigneBonEntrée;
FETCH cr2 INTO codeArtLigneBonSortie, qteLigneBonSortie;
IF no_more_rows THEN
CLOSE cr;
LEAVE the_loop;
END IF;

IF codeArtLigneBonEntrée <> codeArtLigneBonSortie THEN
SET test = FALSE;
END IF;

IF qteLigneBonEntrée <> qteLigneBonSortie THEN
SET test = FALSE;
END IF;

END LOOP the_loop;
END IF;
RETURN test;

END$$

DELIMITER ;

最佳答案

您的所有 DECLARE 必须位于过程的顶部,在任何其他逻辑之前。请尝试以下操作。

DELIMITER $$

CREATE DEFINER=root@localhost FUNCTION verifierQteDemandee(numBonIn INT) RETURNS BOOLEAN
BEGIN
DECLARE numLignesBonEntrée, numLignesBonSortie INTEGER;
DECLARE codeArtLigneBonEntrée, codeArtLigneBonSortie, qteLigneBonEntrée, qteLigneBonSortie INTEGER;
DECLARE no_more_rows BOOLEAN;
DECLARE qteArticle INTEGER;
DECLARE test BOOLEAN DEFAULT TRUE;

-- Moved declarations to before other logic
DECLARE cr1 CURSOR FOR SELECT codeArt, qte FROM LigneBonEntrée WHERE numBon = numBonIn ORDER BY codeArt ASC;
DECLARE cr2 CURSOR FOR SELECT codeArt, qte FROM LigneBonSortieWHERE numBon = (SELECT estLieA FROM LigneBonEntréeWHERE numBon = numBonIn) ORDER BY codeArt ASC;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_rows = TRUE;

SET numLignesBonEntrée = (SELECT COUNT(*) FROM LigneBonEntrée WHERE numBon = numBonIn);

SET numLignesBonSortie = (SELECT COUNT(*) FROM numLignesBonSortie WHERE numBon = (SELECT estLieA FROM LigneBonEntrée WHERE numBon = numBonIn));

IF numLignesBonEntrée <> numLignesBonSortie THEN
SET test = FALSE;
ELSE

OPEN cr1;
OPEN cr2;

the_loop: LOOP
FETCH cr1 INTO codeArtLigneBonEntrée, qteLigneBonEntrée;
FETCH cr2 INTO codeArtLigneBonSortie, qteLigneBonSortie;
IF no_more_rows THEN
CLOSE cr;
LEAVE the_loop;
END IF;

IF codeArtLigneBonEntrée <> codeArtLigneBonSortie THEN
SET test = FALSE;
END IF;

IF qteLigneBonEntrée <> qteLigneBonSortie THEN
SET test = FALSE;
END IF;

END LOOP the_loop;
END IF;
RETURN test;

END$$

DELIMITER ;

Mysql 文档:http://dev.mysql.com/doc/refman/5.0/en/declare.html

DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.

Declarations must follow a certain order. Cursor declarations must appear before handler declarations. Variable and condition declarations must appear before cursor or handler declarations.

关于mysql - 我的 MySQL 脚本语法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17032963/

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