首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >mysql中不同表的多次更新(存储库)

问mysql中不同表的多次更新(存储库)
EN

Stack Overflow用户
提问于 2016-01-19 02:17:15
回答 1查看 18关注 0票数 1

我正在尝试对MYSQL中存储过程中的不同表进行多次更新。但是,如果其中一个更新没有找到ID,它将发出警告或错误。

所以我想用IF ELSE,但我不知道怎么做。

这是存储过程中的代码示例

代码语言:javascript
复制
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;

因此,我需要判断第一次更新是否成功或正确,如果没有,则继续执行另一次更新。

我希望你能帮助我。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2016-01-19 02:19:25

所以它适用于5个表。您可以对其进行扩展。

代码语言:javascript
复制
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 ;

调用它:

代码语言:javascript
复制
CALL setup('xx');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34861424

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档