Ahmed Abdellatif
Programmer
I have a transaction that will be applied to 2 tables. Table 2 has field 'image' that is unique. When trying to execute the following code in the first time, it is executed correctly but when trying to Re-execute it again (to show effect of rollback), a duplicate error occurred in table2 and the transaction was applied correctly to table1 and committed so the table1 committed record was not rolled back. Here is my code:
Code:
DROP PROCEDURE IF EXISTS mprocedure;
DELIMITER $$
CREATE PROCEDURE mprocedure ()
BEGIN
START TRANSACTION;
INSERT INTO `mschema`.`table1`
(`maxbudget`,
`blocked`,
`d_percentage`,
`max discount`)
VALUES
('2250',
'0',
'.9',
'.99');
if (@@error_count = 0 ) then
INSERT INTO `mschema`.`table2`
(`name`,`image`,`date`,`fKey_id`)
values
('jhon','jfdd', '2018-01-01 00:00:00', LAST_INSERT_ID());
if (@@error_count = 0 ) then
commit;
else
rollback;
end if;
else rollback;
end if;
commit;
END;
$$
DELIMITER ;