Mysql trigger from one base to another

Asked

Viewed 324 times

0

I am trying to create a Trigger in Mysql where I need in updating a table (I will call tb1) depending on the situation field, delete a record in another database from a table.

Base 1 = B1
Table 1 = Tb1
Base 2 = B2
Table 2 = Tb2

-- Trigger I’m trying to create

CREATE TRIGGER trigger_agendamento_vinculo_situacao AFTER UPDATE ON B1.TB1 for each row begin
     if (new.situacao <> 'ativo') THEN 
        delete from B2.TB2 where username = NEW.prontuario_original;
  end;

But every time I try to create Mysql it shows the following error:

CREATE TRIGGER B1.TB1.trigger_schedule_vinculo_situacao AFTER UPDATE ON B1.TB1 for each Row Begin if (new.situation <> 'asset') THEN delete from B2.TB2 Where username = NEW.ready_original Error Code: 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 '' at line 3 0.000 sec

  • What version of Mysql database are you using?

  • The Mysql database is '5.5.38-0ubuntu0.12.04.1-log'

  • I think Mysql doesn’t "like" many unnecessary spaces. Check if you happen to have extra spaces...

  • I’ve tried, I’ve done it like it’s a single line, but it doesn’t work.

  • You tried with the DELIMITER?

  • I could not format for better visualization delimiter | &#xA;CREATE TRIGGER trigger_agendamento_vinculo_situacao AFTER UPDATE ON B1.TB1 for each row begin &#xA;if (new.situacao <> 'ativo') THEN delete from B2.TB2 where username = NEW.prontuario_original; end;&#xA; delimiter ;

Show 1 more comment

1 answer

0

I was able to figure out the problem. Since the version of my mysql is 5.5 it has some particularities in the construction. It doesn’t change much, it’s details that affect. You have to use the delimiter and the ; corresponding.

See the code below that it accepts.

Ps.: Now I found another problem that the table already has a Rigger de afeter Insert, then I will have to change the existing one to include the above code, because mysql gives error 1235 stating that it is not possible more than one Rigger for the same action.

Let’s go to the code:

DELIMITER $$
DROP TRIGGER IF EXISTS `B1`.`trigger_update_radius_vinculo`$$
CREATE TRIGGER trigger_update_radius_vinculo AFTER UPDATE ON B1.TB1 for each row 
Begin
   if new.situacao <> 'ativo' THEN 
    delete from B2.TB2 where username = NEW.prontuario_original;
 End if;
End;
$$
DELIMITER ; 
  • Ricardo if you have a new question, create a new question because this area is determined only for answers.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.