MySQL Forums
Forum List  »  Stored Procedures

Re: How to catch SQL Exception Error number in MySQL 5.6
Posted by: Peter Brawley
Date: December 01, 2019 02:19PM

Until you upgrade to 8.0, assuming a target table t( somecol t int ), an errorlog table ( ts timestamp, msg text ), and a desire for handling bad key errors, here's one way ...

delimiter go
CREATE PROCEDURE errdemo( pvalue INT, OUT pResult INT )
BEGIN
  DECLARE bad_key CONDITION FOR SQLSTATE '23000';
  DECLARE EXIT HANDLER FOR bad_key
    BEGIN
      INSERT INTO errlog( dt, msg )
        VALUES( Now(), Concat('Errdemo:Insert on t failed; invalid key.') );
      SET presult=-1;
      ROLLBACK;
    END;
  START TRANSACTION;
    INSERT INTO t VALUES( pvalue, NULL ) ;
    SET pResult=Row_Count();
  COMMIT;
END;
go
delimiter ;



Edited 1 time(s). Last edit at 12/01/2019 02:32PM by Peter Brawley.

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: How to catch SQL Exception Error number in MySQL 5.6
513
December 01, 2019 02:19PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.