im trying to create a stored procedure that have multiples CASE STATEMENTS I have the following stored procedure:
我试图创建一个具有倍数的存储过程CASE STATEMENTS我有以下存储过程:
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?
每次我执行此程序时我得到以下错误是:“CASE语句未找到案例”我做错了什么?
2 个解决方案
#1
19
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:
这是MySQL特有的问题。在MySQL中,case必须始终具有有效匹配,因此是ELSE子句。并且由于匹配后的语句不能为空,您可以用空块填充它,如下所示:
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
1
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.
该错误意味着其中一个'case'语句找不到匹配项。你确定变量olds和news包含正确的值吗?尝试将每种情况下的最后“何时”更改为“其他”,然后查看您的程序是否有效。
#1
19
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:
这是MySQL特有的问题。在MySQL中,case必须始终具有有效匹配,因此是ELSE子句。并且由于匹配后的语句不能为空,您可以用空块填充它,如下所示:
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
1
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.
该错误意味着其中一个'case'语句找不到匹配项。你确定变量olds和news包含正确的值吗?尝试将每种情况下的最后“何时”更改为“其他”,然后查看您的程序是否有效。