Mysql CASE NOT FOUND for CASE STATEMENT on a Stored Procedure

匿名 (未验证) 提交于 2019-12-03 00:48:01

问题:

im trying to create a stored procedure that have multiples CASE STATEMENTS I have the following stored procedure:

BEGIN CASE @olds WHEN 'emp' THEN     CASE @news     WHEN 'loc' THEN         UPDATE equipos SET pe=pe-1,pg=pg+1 WHERE id=@eqloc;         UPDATE equipos SET pe=pe-1,pp=pp+1 WHERE id=@eqvis;         UPDATE partidos SET `eqgan`=@news WHERE id=@mst;         UPDATE log_partidos SET `status`=@news WHERE `match`=@mst;     WHEN 'vis' THEN         UPDATE equipos SET pe=pe-1,pg=pg+1 WHERE id=@eqvis;         UPDATE equipos SET pe=pe-1,pp=pp+1 WHERE id=@eqloc;         UPDATE partidos SET `eqgan`=@news WHERE id=@mst;         UPDATE log_partidos SET `status`=@news WHERE `match`=@mst;     END CASE; WHEN 'loc' THEN     CASE @news     WHEN 'emp' THEN         UPDATE equipos SET pe=pe+1,pg=pg-1 WHERE id=@eqloc;         UPDATE equipos SET pe=pe+1,pp=pp-1 WHERE id=@eqvis;         UPDATE partidos SET `eqgan`=@news WHERE id=@mst;         UPDATE log_partidos SET `status`=@news WHERE `match`=@mst;     WHEN 'vis' THEN         UPDATE equipos SET pp=pp-1,pg=pg+1 WHERE id=@eqvis;         UPDATE equipos SET pg=pg-1,pp=pp+1 WHERE id=@eqloc;         UPDATE partidos SET `eqgan`=@news WHERE id=@mst;         UPDATE log_partidos SET `status`=@news WHERE `match`=@mst;     END CASE; WHEN 'vis' THEN     CASE @news     WHEN 'emp' THEN         UPDATE equipos SET pe=pe+1,pg=pg-1 WHERE id=@eqvis;         UPDATE equipos SET pe=pe+1,pp=pp-1 WHERE id=@eqloc;         UPDATE partidos SET `eqgan`=@news WHERE id=@mst;         UPDATE log_partidos SET `status`=@news WHERE `match`=@mst;     WHEN 'loc' THEN         UPDATE equipos SET pp=pp-1,pg=pg+1 WHERE id=@eqloc;         UPDATE equipos SET pg=pg-1,pp=pp+1 WHERE id=@eqvis;         UPDATE partidos SET `eqgan`=@news WHERE id=@mst;         UPDATE log_partidos SET `status`=@news WHERE `match`=@mst;     END CASE; END CASE; END 

Everytime im executing this procedure i got the following error is: "Case not found for CASE statement" What im doing wrong?

回答1:

This is a MySQL specific problem.
In MySQL, a case must always have a valid match, thus an ELSE clause.
And as the statement after the match cannot be empty, you can fill it up with an empty block like so:

ELSE BEGIN END; 

So a valid case would be, for example:

CASE @news     WHEN 'emp' THEN       UPDATE equipos SET pe=pe+1,pg=pg-1 WHERE id=@eqvis;     WHEN 'loc' THEN       UPDATE equipos SET pp=pp-1,pg=pg+1 WHERE id=@eqloc;     ELSE BEGIN END;   END CASE; 


回答2:

That error means one of the 'case' statements can't find a match. Are you sure variables olds and news contain right values? Try to change last 'when' in each case to 'else' and see if your procedure works then.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!