I have been trying to convert trigger from oracle to MySQL/MariaDB. I'm still new in MySQL so I'm still figuring.
This is the table structure from the table that I'm doing the trigger:
CREATE TABLE `attendance` (
`attendanceid` varchar(10) NOT NULL,
`studentname` varchar(50) DEFAULT NULL,
`classname` varchar(20) DEFAULT NULL,
`status` varchar(20) DEFAULT NULL,
`atdate` varchar(20) DEFAULT NULL,
`classid` varchar(20) DEFAULT NULL
)
I've already created a sequence table for the trigger as below :
CREATE TABLE `att_seq` (
`id` int(11) NOT NULL
)
This is the code from oracle:
CREATE OR REPLACE TRIGGER "STUDENT"."CLASSID"
BEFORE INSERT ON attendance
FOR EACH ROW
declare
BEGIN
if :new.attendanceid is null then
select 'ATT' || attandance_seq.nextval into :new.attendanceid from dual;
if inserting then
if(:new.classname = '4A') then
:new.classid := 'CLASS401';
end if;
if(:new.classname = '4B') then
:new.classid := 'CLASS402';
end if;
if(:new.classname = '4C') then
:new.classid := 'CLASS403';
end if;
end if;
end if;
END;
/
and this is by far how I have converted it (p/s: the 'update class..' code is a new code that i added from the oracle one):
DELIMITER $$
CREATE TRIGGER att_auto_id
BEFORE INSERT ON attendance
FOR EACH ROW
BEGIN
INSERT INTO att_seq VALUES (NULL);
SET NEW.attendanceid = CONCAT('ATT', LPAD(LAST_INSERT_ID(), 3, '00'));
IF inserting THEN
IF new.classname = `4A` THEN
SET new.classid = `CLASS401`;
UPDATE class
SET classid = new.classid;
END IF;
IF new.classname = `4B` THEN
SET new.classid = `CLASS402`;
UPDATE class
SET classid = new.classid;
END IF;
IF new.classname = `4C` THEN
SET new.classid = `CLASS403`;
UPDATE class
SET classid = new.classid;
END IF;
END IF;
END $$
when I run it, I get this error:
1054 - Unknown column 'inserting' in 'field list'
It would be much appreciated if someone could help me. Thank you in advanced :)
LAST_INSERT_IDdoesn't look right, its from the previous insert.OLD.pk IS NULLmight be the way to determineinserting.