Exception handling in MySQL Stored Procedures

 Exception handling in MySQL Stored Procedures

--  Replace the table name,columns names according to your requirement

-- drop procedure sp_med_master_del


DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_med_master_del`(
IN  P_ID INT
)
BEGIN
       
DECLARE exit handler for sqlexception
BEGIN
ROLLBACK;
SELECT 'Delete Medicine  failed ...';
                        RESIGNAL;   -- throws the actual sql error
END;

DECLARE exit handler for sqlwarning
BEGIN
ROLLBACK;
                        SELECT 'Delete Medicine  failed ...';
                        RESIGNAL;            
END;
            
START TRANSACTION;
    
    delete from  `cli.medicine_mstr` 
    WHERE ID=P_ID;
            
COMMIT;
END$$
DELIMITER ;

1 Comments

And that's all there is to it!

If anyone has any other questions or requests for future How To posts, you can either ask them in the comments or email me. Please don't feel shy at all!

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

Previous Post Next Post