views:

47

answers:

1

I got this trigger, I want to evaluate the mobile phone and insert into another table if the regexp returns a value. I've tried with this but not success.

delimiter //

create trigger companyA_phones after insert on customer

for each row

begin

   set @phone = (select new.phone from customer where new.phone regexp '^0416');

   if (@phone) then

      insert into company_A(new.id, @phone);

   end if;

end//
+3  A: 

You don't make it entirely clear what trouble you are having, but I don't think you need to be selecting the .new values like that -- as they are already available to you. Try something like this:

CREATE TRIGGER companyA_phones
AFTER INSERT ON customer
FOR EACH ROW
BEGIN
  IF (new.phone REGEXP '^0416' AND new.id IS NOT NULL) THEN
    INSERT INTO company_A (customerid, phone)
         VALUES (new.id, new.phone);
  END IF;
END

The need for this trigger does seem to suggest that your underlying schema design is not correctly normalised, so you might want to think about that too.

Tom
Thanks. I had a terrible error with that trigger, I just need to evaluate the phone with the regexp. Thanks.
Felix Guerrero