我有一个CASE
语句,它接受表的第一个 40 记录,它有基于计数器的不同程序 - @COUNT
。
不幸的是,当我在没有else
的情况下运行该程序时,它不起作用并提供Case not found for CASE statement (Error 1339)
。
但是当我确实有其他功能时,它可以正常运行,但它会在最后一天运行两次,所以我有一个重复的最后日期。
有什么理由吗?请检查下面的代码..已经删除了很多
CREATE PROCEDURE `init`(In dID INT)
BEGIN
DECLARE first40 CURSOR FOR
SELECT * FROM tbl
WHERE tbl.id = dID
ORDER BY tbl.datum ASC
LIMIT 40;
OPEN first40;
SET @COUNT = 0;
REPEAT FETCH first40 INTO Tid, Tdt, Tcls, Thgh, Tlw, Tvol, Tnms_id;
SET @COUNT = @COUNT + 1;
CASE
WHEN @COUNT = 1 THEN
WHEN @COUNT>= 2 AND @COUNT < 9 THEN
WHEN @COUNT = 9 THEN
WHEN @COUNT >= 10 AND @COUNT < 12 THEN
WHEN @COUNT = 12 THEN
WHEN @COUNT = 13 THEN
WHEN @COUNT = 14 THEN
WHEN @COUNT = 15 THEN
WHEN @COUNT = 16 THEN
WHEN @COUNT >= 17 AND @COUNT < 20 THEN
WHEN @COUNT >= 20 AND @COUNT < 26 THEN
WHEN @COUNT = 26 THEN
WHEN @COUNT >= 27 AND @COUNT < 34 THEN
WHEN @COUNT = 34 THEN
WHEN @COUNT > 34 AND @COUNT <= 40 THEN
END CASE;
INSERT INTO .....
VALUES ....;
UNTIL done END REPEAT;
CLOSE first40;
END
答案 0 :(得分:2)
您应该能够轻松地将ELSE
添加回CASE(与SELECT查询不同,ELSE在存储过程CASE块中必需),然后在此之前跟进插入:
IF @COUNT <= 40
INSERT INTO .....
VALUES ....;
重复第40行的原因是,在第41次通过REPEAT FETCH
时,它没有获得任何数据