Update in two SQL Server tables

Asked

Viewed 505 times

3

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

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

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

update vinculoreferencia set taridinclusao = 168228 where tarid = 168261.
  • With Stored Procedure you can do this.

1 answer

1


Among its tables, which has the primary key?

If it is tarid, create the Foreign key with VinculoReferencia as Scade on update, thus while updating tarid, Taridinclusao will also be updated.

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

References:

Browser other questions tagged

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