2

我在 GoDaddy 上通过 phyMyAdmin 使用 MySQL。我可以执行这个语句,返回一个整数值,没有任何问题:

SELECT DISTINCT `runNumber` FROM RideSlot WHERE (timeRange = 2) LIMIT 0,1;

但是,当我尝试创建以下存储过程时,出现以下错误:

1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 25 行的 '' 附近使用正确的语法

DROP PROCEDURE IF EXISTS `spGetPreferredRunNumbers`$$
CREATE PROCEDURE `spGetPreferredRunNumbers`(IN prefTimeRange INT(4), INOUT run1 INT(4), INOUT run2 INT(4), INOUT run3 INT(4), INOUT run4 INT(4), INOUT run5 INT(4))
BEGIN
IF (prefTimeRange = 2) THEN BEGIN
SELECT DISTINCT `runNumber` FROM RideSlot WHERE (timeRange = prefTimeRange) LIMIT 0,1  INTO run1;
SELECT DISTINCT `runNumber` FROM RideSlot WHERE (timeRange = prefTimeRange) LIMIT 1,1  INTO run2;
SELECT DISTINCT `runNumber` FROM RideSlot WHERE (timeRange = prefTimeRange) LIMIT 2,1  INTO run3;
SELECT DISTINCT `runNumber` FROM RideSlot WHERE (timeRange = prefTimeRange) LIMIT 3,1  INTO run4;
SET run5 = -1;
END;
ELSE IF (prefTimeRange = 3) THEN BEGIN
SELECT DISTINCT `runNumber` FROM RideSlot WHERE (timeRange = prefTimeRange) LIMIT 0,1  INTO run1;
SELECT DISTINCT `runNumber` FROM RideSlot WHERE (timeRange = prefTimeRange) LIMIT 1,1  INTO run2;
SELECT DISTINCT `runNumber` FROM RideSlot WHERE (timeRange = prefTimeRange) LIMIT 2,1  INTO run3;
SELECT DISTINCT `runNumber` FROM RideSlot WHERE (timeRange = prefTimeRange) LIMIT 3,1  INTO run4;
SET run5 = -1;
END;
ELSE BEGIN
SELECT DISTINCT `runNumber` FROM RideSlot WHERE (timeRange = prefTimeRange) LIMIT 0,1  INTO run1;
SELECT DISTINCT `runNumber` FROM RideSlot WHERE (timeRange = prefTimeRange) LIMIT 1,1  INTO run2;
SELECT DISTINCT `runNumber` FROM RideSlot WHERE (timeRange = prefTimeRange) LIMIT 2,1  INTO run3;
SELECT DISTINCT `runNumber` FROM RideSlot WHERE (timeRange = prefTimeRange) LIMIT 3,1  INTO run4;
SELECT DISTINCT `runNumber` FROM RideSlot WHERE (timeRange = prefTimeRange) LIMIT 4,1  INTO run5;
END;
END IF;
END$$
DELIMITER;

是的,我已经单独测试了具有不同限制的其他选择语句,它们也可以正常工作,除非在选择 LIMIT 4,1 时使用 2 或 3 作为 prefTimeRange。prefTimeRange 的 2 和 3 仅返回四个结果,因此如果 prefTimeRange 为 2 或 3,我将 -1 分配给 run5。

无论我是否这样做,我都会得到同样的错误:prefTimeRange = 3 或者这个: prefTimeRange = "3"

是的,我记得告诉 phpMyAdmin 正确的分隔符是 $$。

我错过了什么????

4

2 回答 2

0

已解决:事实证明 ELSE 和 IF 之间的空间需要删除!

于 2013-04-15T22:14:54.310 回答
0

这是一个非常糟糕的 mysql 问题,并且没有正确记录。MYSQL 存储过程 - 如果您在 ELSE 之前留下额外的行或空格,则 else 部分无论如何都会执行。

IF <your condition> then
<do this code>
                   <-- extra empty line
ELSE
<this always executes no matter what is in IF statement>

在上面的示例中 - 如果您在 ELSE 上方留下一个空行,则 else 部分始终执行。不要在 if-else 的 else 部分上方留下 EXTRA LINE。

于 2016-09-25T23:19:33.207 回答