如何在没有存储过程的情况下有条件地在 MySQL 中引发错误

Gen*_*ent 8 mysql condition errors raiserror

我需要有条件地引发错误,但我只能使用简单的语句,而不能使用存储过程。

我想做这样的事情:

select case when foo = "bar" then 1 else SIGNAL SQLSTATE 'ERROR' end;
Run Code Online (Sandbox Code Playgroud)

不幸的是,SIGNAL 仅可用于触发器和过程,我必须在现有应用程序中使用它,该应用程序只允许我输入语句,但不允许我输入过程。(我只有一条长线,无法设置分隔符等)

有没有其他方法可以有条件地导致运行时错误?

Rol*_*DBA 10

从 else 部分具有多行的表中检索列

select case when foo = "bar" then 1 else (select table_name from information_schema.tables) end;
Run Code Online (Sandbox Code Playgroud)

例如,让我们使用 @foo 而不是 foo

mysql> select case when @foo = "bar" then 1 else (select table_name from information_schema.tables) end;
ERROR 1242 (21000): Subquery returns more than 1 row
Run Code Online (Sandbox Code Playgroud)

试一试 !!