lundi 12 juin 2017

Trigger to create varchar auto increment with if else statement

Hi Does anybody know how I should create a trigger that will create a unique id from varchar and auto increment table including if else from user input I was planning something similar to the below code but i am getting an error 1050 table user_increment_table exist

DELIMITER ;
CREATE TRIGGER TG_ID
BEFORE INSERT ON userdatabase
FOR EACH ROW
BEGIN
  if User_department like "HR"
  then
  INSERT INTO user_increment_table VALUES (NULL);
  SET NEW.User_ID = CONCAT('HRU', LPAD(LAST_INSERT_ID(), 3, '0'));

Elseif U_role like "MARKETING" 
then  
  INSERT INTO user_increment_table VALUES (NULL);
  SET NEW.User_ID = CONCAT('MRK', LPAD(LAST_INSERT_ID(), 3, '0'));

END$$
DELIMITER ;

Aucun commentaire:

Enregistrer un commentaire