Ale*_*lev 2 mysql sql select sql-scripts
我需要编写 SQL 脚本,如果在数据库中找不到某些值,该脚本就会停止。像这样的东西(伪代码):
BEGIN;
...
set @a = (select ... );
if @a IS NULL THEN STOP_WITH_ERROR_AND_ROLLBACK();
...
COMMIT;
Run Code Online (Sandbox Code Playgroud)
有人可以帮助我吗?
更新:由于某些原因,我无法使用存储过程或函数。
更新2:注意:我不需要显式回滚。脚本执行的中断就足够了。它自动回滚未提交事务的更改。
start transaction;
set @a=(select ...);
-- whatever you want to execute your code like insert, update
set @b=(select if(@a is null,'ROLLBACK','COMMIT'));
-- @b is store **ROLLBACK** if @a is null nither store **COMMIT**
prepare statement1 from @b;
-- now statement1 store as @a value
execute statement1;
Run Code Online (Sandbox Code Playgroud)
我希望它能解决你的问题。