9

I'm using mysql v5.1.48 and red http://dev.mysql.com/doc/refman/5.5/en/signal.html. But the code

DELIMITER $$
CREATE PROCEDURE `CoreRaiseError`()
BEGIN
  SIGNAL SQLSTATE '45000'
      SET MESSAGE_TEXT = 'An error occurred', MYSQL_ERRNO = 1001;
END$$

raise an

SQL Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SQLSTATE '45000'

From which version SIGNAL start to be a keyword? How can I raise an exception with prior version of mysql?

Thank you.

MUY Belgium
  • 2,330
  • 4
  • 30
  • 46

1 Answers1

12

As stated in the manual for MySQL version 5.1:

Other statements related to conditions are SIGNAL, RESIGNAL, and GET DIAGNOSTICS. The SIGNAL and RESIGNAL statements are not supported until MySQL 5.5. The GET DIAGNOSTICS statement is not supported until MySQL 5.6.

To raise an error in older versions of MySQL, just deliberately issue an erroneous command. I often CALL a non-existent procedure, for example:

CALL raise_error;
eggyal
  • 122,705
  • 18
  • 212
  • 237
  • 1
    I had to alter a nice trigger I made to prevent deletion of certain kinds of records. Tested it in 5.6 and all was cool. Then I found out deploying to 5.1 broke so this worked for me. What I did was: `call You_may_not_delete_those_kind_of_records`. lol. Ugly..but works and makes it easy to find in my logs. – cbmeeks May 08 '13 at 18:24
  • Here is a question you might enjoy: [mysql 5.1 signal an error to cause PDO exceptions](http://stackoverflow.com/questions/18070473/mysql-5-1-signal-an-error-to-cause-pdo-exceptions). – Ggicci Mar 16 '16 at 13:53