I have created this trigger for automatically generating user codes like SDA0001, SDA0002…
I have just included the main statements between BEGIN and END. It is create BEFORE INSERT on table agent_mst
DECLARE max_id INT;
SET max_id=(SELECT MAX(agent_id_pk)+1 FROM `agent_mst`);
IF (max_id IS NULL) THEN
SET max_id=1;
END IF;
SET
NEW.date_added=NOW(),
NEW.date_updated=NOW(),
NEW.agent_code = CONCAT('SDA', LPAD(max_id, 4,'0'));
The error it is giving is as below
MySQL said: #1064 – You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near ‘DECLARE max_id INT; SET max_id=(SELECT
MAX(agent_id_pk)+1 FROMagent_mst); ‘ at line 1
I am creating this Trigger using phpMyAdmin.
Any suggestion what is wrong going here?
Thanks in advance.
This should work: