Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IF statement inside MySQL trigger

Tags:

mysql

triggers

I need some help to complete this trigger. I try it but I don't know what is wrong.

This is the code:

delimiter $$

CREATE TRIGGER attendance_clock AFTER INSERT ON alldevicerecord
FOR EACH ROW
BEGIN
    DECLARE check INT;
    SET check= 1;
    IF (check % 2 == 0) THEN
      INSERT into designation(emp_id, clock_date, clock_in) VALUES (new.id, new.time, new.time);
      check  = check + 1;
    ELSE
      UPDATE designation SET clock_out = new.time WHERE emp_id = NEW.id;
      check = check + 1;
    END IF
END$$

delimiter ;


Anyone can help me please?

like image 947
Husam Avatar asked Apr 21 '12 11:04

Husam


1 Answers

delimiter $$

CREATE TRIGGER attendance_clock AFTER INSERT ON alldevicerecord
FOR EACH ROW
BEGIN
  DECLARE `check` INT;
  SET `check` = 1;
  IF (`check` % 2 = 0) THEN
    INSERT INTO designation (emp_id, clock_date, clock_in) VALUES(new.id, new.time, new.time);
    SET `check`  = `check` + 1;
  ELSE
    UPDATE designation SET clock_out = new.time WHERE emp_id = NEW.id;
    SET `check`  = `check` + 1;
  END IF;
END$$

delimeter ;
like image 194
juergen d Avatar answered Sep 18 '22 15:09

juergen d