I created a Before Insert trigger that generates a unique code for update on my table companystructures (in mysql). I keep getting when i try submitting my form.
Can't update table 'companystructures' in stored function/trigger because it is already used by statement which invoked this stored function/trigger
I'm quite new to this.
BEGIN
DECLARE cd_count int;
SET cd_count=0;
IF NEW.type = 'Department' THEN
INSERT INTO companystructures SET companystructures.comp_code = concat('D',(SELECT count(*) from companystructures WHERE type = 'Department')+1);
ELSEIF NEW.type = 'Unit' THEN
INSERT INTO companystructures SET companystructures.comp_code = concat('U', (SELECT count(*) from companystructures WHERE type = 'Unit')+1);
ELSEIF NEW.type = 'Regional Office' THEN
INSERT INTO companystructures SET companystructures.comp_code = concat('R', (SELECT count(*) from companystructures WHERE type = 'Regional Office')+1);
ELSEIF NEW.type = 'Head Office' THEN
INSERT INTO companystructures SET companystructures.comp_code = concat('H', (SELECT count(*) from companystructures WHERE type = 'Head Office')+1);
ELSEIF NEW.type = 'Company' THEN
INSERT INTO companystructures SET companystructures.comp_code = concat('C', (SELECT count(*) from companystructures WHERE type = 'Company')+1);
END IF;
END