How to raise an error within a MySQL function

Dónal picture Dónal · Jan 21, 2009 · Viewed 87.2k times · Source

I've created a MySQL function and would like to raise an error if the values passed for the parameters are invalid. What are my options for raising an error within a MySQL function?

Answer

Austin Hyde picture Austin Hyde · Aug 6, 2012

MySQL 5.5 introduces signals, which are similar to exceptions in other languages:

http://dev.mysql.com/doc/refman/5.5/en/signal.html

For example, in the mysql command line client:

mysql> SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Custom error';
ERROR 1644 (45000): Custom error