Skip to content

Instantly share code, notes, and snippets.

@theory
Last active December 19, 2015 05:59
Show Gist options
  • Select an option

  • Save theory/5908699 to your computer and use it in GitHub Desktop.

Select an option

Save theory/5908699 to your computer and use it in GitHub Desktop.
A MySQL function that throws an error in a `SELECT` statement will have its error suppressed in a `DO` statement.
DELIMITER |
CREATE FUNCTION checkit(doit INTEGER, message VARCHAR(256)) RETURNS INTEGER
BEGIN
IF doit IS NULL OR doit = 0 THEN
SIGNAL SQLSTATE 'ERR0R' SET MESSAGE_TEXT = message;
END IF;
RETURN doit;
END;
|
DELIMITER ;
select checkit(NULL, 'foo');
do checkit(NULL, 'foo');
-- Output:
-- Query OK, 0 rows affected (0.00 sec)
--
-- ERROR 1644 (ERR0R): foo
--
-- Query OK, 0 rows affected, 1 warning (0.00 sec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment