Nov 1, 2018

How to cancel operation and throw an error in MySQL

SIGNAL is the way to return an error. SIGNAL provides error information to a handler, to an outer portion of the application, or to the client. Also, it provides control over the error's characteristics (error number, SQLSTATE value, message). Without SIGNAL, it is necessary to resort to workarounds such as deliberately referring to a nonexistent table to cause a routine to return an error.

To signal a generic SQLSTATE value, use '45000', which means unhandled user-defined exception.

-- sql statements
signal sqlstate '45000';
-- sql statements