0

我有一条CASE语句,它接收表的前40条记录,它具有基于计数器的不同过程 - @COUNT。不幸的是,当我在没有 a 的情况下运行该程序时,else它不起作用并给出Case not found for CASE statement (Error 1339).

但是当我确实有 else 它工作正常但它在最后一天运行两次,所以我有一个最后日期的副本。

这有什么原因吗?请检查我下面的代码..很多已被删除

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
4

1 回答 1

2

您应该能够轻松地将ELSE后面添加到 CASE(与 SELECT 查询不同,存储过程 CASE 块中需要ELSE ),然后在 INSERT INTO 之前跟进:

IF @COUNT <= 40
INSERT INTO .....
            VALUES ....;

重复第 40 行的原因是在第 41 次通过时REPEAT FETCH,它没有得到任何数据,所以

  1. 变量未更新(但也未清除)
  2. 直到您在该部分之前的代码块之后才检查条件UNTIL done
于 2013-04-20T11:39:39.413 回答