HiveBrain v1.2.0
Get Started
← Back to all entries
debugsqlModerate

How to conditionally raise an error in MySQL without stored procedure

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
conditionallystoredwithouterrorproceduremysqlraisehow

Problem

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 ?

Solution

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 !!

Code Snippets

select case when foo = "bar" then 1 else (select table_name from information_schema.tables) end;
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

Context

StackExchange Database Administrators Q#78594, answer score: 14

Revisions (0)

No revisions yet.