我正在尝试对MYSQL中存储过程中的不同表进行多次更新。但是,如果其中一个更新没有找到ID,它将发出警告或错误。
所以我想用IF ELSE,但我不知道怎么做。
这是存储过程中的代码示例
be is an entrie parameter.
update table1 set status = 2 where id_be = be;
update table2 set status = 2 where id_be = be;
update table3 set status = 2 where id_be = be;
update table4 set status = 2 where id_be = be;
update table5 set status = 2 where id_be = be;
update table6 set status = 2 where id_be = be;
update table7 set status = 2 where id_be = be;
update table8 set status = 2 where id_be = be;
update table9 set status = 2 where id_be = be;因此,我需要判断第一次更新是否成功或正确,如果没有,则继续执行另一次更新。
我希望你能帮助我。谢谢!
发布于 2016-01-19 02:19:25
所以它适用于5个表。您可以对其进行扩展。
DELIMITER ;;
CREATE DEFINER=`root`@`127.0.0.1` PROCEDURE `setup`(IN be CHAR(200))
BEGIN
DECLARE countRow INT;
UPDATE table1 SET STATUS=2 WHERE id_be = be;
SET countRow = ROW_COUNT();
IF(countRow = 0) THEN
UPDATE table2 SET STATUS=2 WHERE id_be = be;
END IF;
SET countRow = ROW_COUNT();
IF(countRow = 0) THEN
UPDATE table3 SET STATUS=2 WHERE id_be = be;
END IF;
SET countRow = ROW_COUNT();
IF(countRow = 0) THEN
UPDATE table4 SET STATUS=2 WHERE id_be = be;
END IF;
SET countRow = ROW_COUNT();
IF(countRow = 0) THEN
UPDATE table5 SET STATUS=2 WHERE id_be = be;
END IF;
END;;
DELIMITER ;调用它:
CALL setup('xx');https://stackoverflow.com/questions/34861424
复制相似问题