Guest User

Untitled

a guest
Aug 19th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. MySQL Stored Procedure Error Handling
  2. DELIMITER $$
  3.  
  4. CREATE PROCEDURE `myProcedure`(OUT o_error_status varchar(50))
  5. MY_BLOCK: BEGIN
  6.  
  7. DECLARE EXIT handler for 1062 set o_error_status := "Duplicate entry in table";
  8. DECLARE EXIT handler for 1048 set o_error_status := "Trying to populate a non-null column with null value";
  9. -- declare handlers ad nauseum here....
  10.  
  11. DECLARE EXIT handler for sqlexception set o_error_status:= "Generic SQLException. You'll just have to figure out the SQLSTATE yourself...." ;
  12.  
  13. -- Procedure logic that might error to follow here...
  14.  
  15. END MY_BLOCK$$
  16.  
  17. SHOW ERRORS LIMIT 1 -- for SQL-state > 2
  18. SHOW WARNINGS LIMIT 1 -- for SQL-state 1,2
  19.  
  20. DECLARE EXIT HANDLER FOR SQLSTATE SQLEXCEPTION .....;
  21.  
  22. SELECT RAISE_ERROR_unable_to_update_basket;
  23.  
  24. ERROR 1054 (42S22): Unknown column 'RAISE_ERROR_unable_to_update_basket' in 'field list'
Add Comment
Please, Sign In to add comment