Update on two SQL Server tables [duplicate]

3

How do I perform an update at the same time on two different tables?

I have a table named Tarefa and another called VinculoReferencia , both have the field Taridinclusao where is this field that needs to be updated at the same time, and the TarID field, which is the reference of the tables.

In the query below is a simple update if it were to update only in the vinculoreferencia table, but also when updating this, also update in the task table in the taridinclusao field:

update vinculoreferencia set taridinclusao = 168228 where tarid = 168261.
    
asked by anonymous 02.10.2017 / 22:56

1 answer

1

Among your tables, which one defines the primary key?

In case of being tarid , create the foreign key with VinculoReferencia as cascade on update , so updating tarid , Taridinclusao will also be updated.

ALTER TABLE VinculoReferencia
ADD CONSTRAINT FK_Taridinclusao
    FOREIGN KEY (Taridinclusao)
    REFERENCES Tarefa(TarID)
    ON UPDATE CASCADE;

References:

02.10.2017 / 23:08