How to conditionally raise an error in MySQL without stored procedure
Domanda
I need to conditionally raise an error, but I can only use a simple statement and no stored procedure.
I'd like to do something like this:
select case when foo = "bar" then 1 else SIGNAL SQLSTATE 'ERROR' end;
Unfortunately SIGNAL is only usable in triggers and procedures and I have to use this within an existing application that only allows me to enter statements, but not procedures. (I only have one long line and no way to set a DELIMITER etc.)
Is there any other way to conditionally cause a runtime error ?
Soluzione
Retrieve column from a table with multiple rows in the else part
select case when foo = "bar" then 1 else (select table_name from information_schema.tables) end;
As an example, let's use @foo instead of 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
Give it a Try !!
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a dba.stackexchange