MySql 条件停止脚本执行
MySql conditional stop script execution
我需要编写 SQL 脚本,如果在数据库中找不到某些值,该脚本就会停止。
像这样(伪代码):
BEGIN;
...
set @a = (select ... );
if @a IS NULL THEN STOP_WITH_ERROR_AND_ROLLBACK();
...
COMMIT;
有人可以帮我吗?
更新: 由于某些原因,我无法使用存储过程或函数。
更新 2: 注意:我不需要显式回滚。脚本执行中断就足够了。它会自动回滚未提交事务的更改。
这应该让你继续。研究一下如何使用事务 mySQL。
START TRANSACTION
...
set @a = (select ... );
IF @a IS NULL
THEN
SELECT 'Error Message' as ErrorMsg
ROLLBACK;
END IF;
...
COMMIT;
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;
希望能解决您的问题。
我需要编写 SQL 脚本,如果在数据库中找不到某些值,该脚本就会停止。 像这样(伪代码):
BEGIN;
...
set @a = (select ... );
if @a IS NULL THEN STOP_WITH_ERROR_AND_ROLLBACK();
...
COMMIT;
有人可以帮我吗?
更新: 由于某些原因,我无法使用存储过程或函数。
更新 2: 注意:我不需要显式回滚。脚本执行中断就足够了。它会自动回滚未提交事务的更改。
这应该让你继续。研究一下如何使用事务 mySQL。
START TRANSACTION
...
set @a = (select ... );
IF @a IS NULL
THEN
SELECT 'Error Message' as ErrorMsg
ROLLBACK;
END IF;
...
COMMIT;
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;
希望能解决您的问题。