我试图在 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;
所以,如果第一次更新成功或为真,我需要离开存储过程,如果没有,继续另一个。
我希望你能帮助我。谢谢!!!
最佳答案
所以它适用于 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');
关于mysql - mysql中不同表的多次更新(存储过程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34861424/