mysql - mysql中不同表的多次更新(存储过程)

标签 mysql stored-procedures sql-update

我试图在 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/

相关文章:

mysql - 将表 1 的单位列更新为表 2

mysql - 更新时阻止 MySQL 读取

java - 如何使用带有 "select"关键字的 MySql 存储过程迭代 ResultSet?

postgresql - 如何在postgres中执行存储过程的字符串结果

java - Spring - 使用 hibernate 和 spring security 扩展类

mysql - 计算具有不同列的行数

mysql - mysql中的存储过程

sql - 是否可以使用与外键连接的表中的数据更新行?

MySQL删除表中的重复行

mysql - BASH 脚本 - 通过 ssh 连接并运行 mysql 命令