3

Here is a trigger i created and i want to compare new.utype with employee as given below but dont know the actual way to compare it in mysql .. how can i achieve this and also please tell if condition syntax is correct or not

delimiter $$

CREATE TRIGGER insdetail_tomaster
AFTER INSERT ON tempdetail
FOR EACH ROW BEGIN
IF(new.utype =='employee') THEN
INSERT INTO master_employee values(new.field1,new.filed2);
ELSE 
INSERT INTO master_manager values(new.field1,new.fiel2);
END $$

DELIMITER ;
3
  • Please help me is there any one here Commented Mar 30, 2014 at 12:42
  • Looks ok, except for new.filed2 should be new.field2 and new is normally written NEW. What's the exact problem you are facing? Does the trigger work as expected? Commented Mar 30, 2014 at 13:11
  • thanks the problem is over i used strcmp function for comparing Commented Mar 30, 2014 at 14:34

2 Answers 2

2

Use "like" key word as below

IF new.utype like 'employee' 

Entire trigger looks like this

delimiter $$

CREATE TRIGGER insdetail_tomaster
AFTER INSERT ON tempdetail
FOR EACH ROW BEGIN
IF new.utype like 'employee'  THEN
INSERT INTO master_employee values(new.field1,new.filed2);
ELSE 
INSERT INTO master_manager values(new.field1,new.fiel2);
END $$

DELIMITER ;

I assumed trigger you wrote other than the string comparison is correct Thanks

Sign up to request clarification or add additional context in comments.

Comments

0

You need to use single = instead of ==

IF new.utype = 'employee' 
DELIMITER $$

CREATE TRIGGER insdetail_tomaster
AFTER INSERT ON tempdetail
FOR EACH ROW BEGIN
  IF new.utype = 'employee' THEN
    INSERT INTO master_employee values(new.field1,new.filed2);
  ELSE 
    INSERT INTO master_manager values(new.field1,new.fiel2);
END $$

DELIMITER ;

You can also use <> for comparing not equal value.

IF new.utype <> 'employee'
DELIMITER $$

CREATE TRIGGER insdetail_tomaster
AFTER INSERT ON tempdetail
FOR EACH ROW BEGIN
  IF new.utype <> 'employee' THEN
    INSERT INTO master_employee values(new.field1,new.filed2);
  ELSE 
    INSERT INTO master_manager values(new.field1,new.fiel2);
END $$

DELIMITER ;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.