mardi 28 juin 2016

MySQL Transaction returns error

So here's the stored procedure I've written. When I ran the DELETE and UPDATE in a single sql tab

as:

DELETE FROM curriculumsubjects WHERE curriculumId = 27;
INSERT INTO curriculumsubjects(curriculumId,subjectCode)
VALUES(27,'MATH101');

it works. It executes delete and insert without any problem

But if I call the stored procedure as:

CALL `enrollmentdb`.`updateCurriculumSubjects`(27, 'MATH101'); 

it returns the 'error' string i put during ROLLBACK

What could be causing the failure of transaction within the stored procedure body when it runs successfully if ran without stored procedure CALL?

Here's the stored procedure.

CREATE DEFINER=`root`@`localhost` PROCEDURE `updateCurriculumSubjects`(IN p_curriculumId int, IN p_subjectCode varchar(100))
BEGIN
    DECLARE hasError BOOLEAN DEFAULT 0;
    DECLARE CONTINUE HANDLER FOR sqlexception SET hasError = 1;

    START TRANSACTION;
        DELETE FROM curriculumsubjects WHERE curriculumId = p_curriculumId;
        INSERT INTO curriculumsubjects(curriculumId,subjectCode)
                                VALUES(p_curriculumId,p_subjCode);
    IF hasError THEN
        ROLLBACK;
        SELECT 'error';
    ELSE
        COMMIT;

    END IF;
END

By the way I'm using Mysql Workbench 6.3 and what I'm trying to do is to delete all the columns matching the curriculumId before I insert again.

On Java, I'll be iterating the call to the stored procedure for multiple inserts.

I hope you can help. I just can't find a reason why delete and insert won't work if put within a transaction.

Thanks.

Aucun commentaire:

Enregistrer un commentaire