没有从存储过程中获取的行

时间:2016-10-17 10:36:37

标签: mysql

我创建了一个这样的存储过程,当我尝试调用它时显示

call points;
$$
329 (02000): No data - zero rows fetched, selected, or processed

此存储过程中的错误是什么?

DELIMITER $$
CREATE PROCEDURE POINTS()

    BEGIN    
        DECLARE NAMEEE VARCHAR(15);    
        declare monthcount int(10);    
        DECLARE CUR2 CURSOR for SELECT * FROM TEMP;    
        open CUR2;   
        read_loop: LOOP   
            FETCH CUR2  INTO NAMEEE;   
            select monthlycount into monthcount from incident  where name=NAMEEE;    
            if monthcount <= 3   then    
                UPDATE INCIDENT SET POINTS="10" WHERE NAME=NAMEEE;    
            elseif (monthcount > 3 and monthcount <=6) then    
                UPDATE INCIDENT SET POINTS="20" WHERE NAME=NAMEEE;   
            elseif (monthcount > 6 and monthcount <=9) then    
                UPDATE INCIDENT SET POINTS="30" WHERE NAME=NAMEEE;   
            elseif (monthcount >9 and monthcount <=12) then    
                UPDATE INCIDENT SET POINTS="40" WHERE NAME=NAMEEE;   
            elseif (monthcount >12 ) then    
                UPDATE INCIDENT SET POINTS="50" WHERE NAME=NAMEEE;    
            end if;   
        END LOOP;   
        CLOSE CUR2;   
    end $$

这是我的表格

CREATE TABLE IF NOT EXISTS INCIDENT(
NAME VARCHAR(45)  NOT NULL,
DAILYCOUNT INT(10),
WEEKLYCOUNT INT(10),
MONTHLYCOUNT INT(10),
POINTS INT(10)  NOT NULL
);

insert into incident values("peter","1","2","3","0");
insert into incident values("thomas","1","2","4","0");
insert into incident values("franklins","1","2","6","0");
insert into incident values("yedhu","1","2","8","0");

CREATE TABLE IF NOT EXISTS TEMP
(
NAME VARCHAR(15)
);

insert into  temp values("peter");
insert into  temp values("thomas");
insert into  temp values("franklins");
insert into  temp values("yedhu");

1 个答案:

答案 0 :(得分:0)

您可以使用单个多表更新语句表达整个存储过程。 update语句在名称和名称字段上连接2个表,并根据monthcount字段中的值更新points字段。 ceil()有效地整理了分工的结果。 if()函数将上限引入12。

update incident inner join temp on incident.name=temp.namee
set incident.points=if(incident.monthcount<=12,ceil(incident.monthcount / 3) * 10, 50)

因此,您可以避免使用复杂的存储过程并将其替换为s