首页 > 解决方案 > mysql 使用 IF NOT NULL 作为事务的一部分

问题描述

我正在使用 MySQL 事务。如果初始 where myHistoryNum结果(匹配名称+rowActive)不为NULL,则继续事务。

我尝试了各种组合,但是无法让insert语句在设置时触发myHistoryNum

history | uuid | name  | rowActive
24      | abcd | Art   | 1          <<< is match, trigger an insert
999     | zxcv | Art   | 0          <<< no match, do not trigger insert


start transaction; 
select @myHistNum := max(history), @olduuid := uuid 
from mytable WHERE (name=art AND rowActive=1);

# START IS NOT NULL test
CASE WHEN @myHistNum IS NOT NULL THEN
set @histNum = @histNum  + 1;
INSERT INTO mytable 
.....
END;                                      <<<ALT, tried END AS;
commit;

也试过

IF @myHistNum IS NOT NULL
 THEN 
   .....
END IF;
commit;

标签: mysql

解决方案


如果我没记错的话,控制流语句(如 IF、WHEN)只能在 MySQL 的存储程序中使用


推荐阅读