Sample transactional stored procedure

 
CREATE DEFINER=`root`@`localhost` PROCEDURE `Sample`(IN inParameterID VARCHAR(10))
Sample:BEGIN
 
DECLARE httpStatus INT DEFAULT 500;
DECLARE errorCode INT DEFAULT NULL;
DECLARE errorDetails TEXT DEFAULT NULL;
 
DECLARE EXIT HANDLER FOR SQLEXCEPTION, SQLWARNING
BEGIN
    ROLLBACK;
    SELECT 500 AS statusCode, 'Failed' AS statusMessage;
END;
 
SET @variableID = 0;
 
SELECT  TableA.ID INTO @variableID
FROM TableA  
WHERE TableA.ParameterID = inParameterID;
 
IF @variableID = 0 THEN
    	SELECT 500 AS errorCode, 'Transaction failed' AS errorMessage;
	LEAVE T2activatePersonStandby;
END IF;
 
START TRANSACTION;
 
UPDATE TableA;
 
UPDATE TableB;
 
UPDATE TableC;
 
END IF;
 
COMMIT;
 
SELECT 200 AS statusCode, 'Success' AS statusMessage;
 
END

Leave a Reply