我在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;

是的,我已经分别测试了其他具有不同限制的select语句,它们也工作得很好,除非在选择LIMIT 4,1时,使用2或3作为prefTimeRange。prefTimeRange的2&3只返回四个结果,因此如果prefTimeRange是2或3,我将-1分配给run5。
无论我是否执行此操作:prefTimeRange = 3或此操作:prefTimeRange = "3"
是的,我记得告诉phpMyAdmin正确的分隔符是$$。
我错过了什么????

最佳答案

解决方案:如果需要移除ELSE和IF之间的空间!

09-05 06:20