UPDATE with multiple records from another table

1

I have a T1 table where I have ID .
I have a T2 table where it gets the ID_T1 ( Foreign Key ) of T1 .
I have a T3 table where it gets the ID_T1 ( Foreign Key ) of the T1 and the ID_T2 ( Foreign Key .

In the T2 table, the IDs of the T3 are right, however, the T1 I created after these registers have been inserted, that is, they are with the records of the T3 to ID_T2 column. Is there any way I can do some NULL in UPDATE , passing all T3 IDs in the correct way?

Here is the code I'm trying to do:

UPDATE  T3 
SET     T3.ID_T2 =  (
                        SELECT      T2.ID 
                        FROM        T2 
                        INNER JOIN  T3 ON T2.ID_T1 = T3.ID_T1
                    ) 
WHERE   T3.ID_T1 =  (
                        SELECT      T1.ID 
                        FROM        T1 
                        INNER JOIN  T3 ON T3.ID_T1 = T1.ID 
                        INNER JOIN  T2 ON T2.ID_T1 = T3.ID_T1
                    )

In SQL Server 2008.

    
asked by anonymous 11.07.2018 / 15:52

1 answer

0

This query might solve your problem:

UPDATE      T3 
SET         T3.ID_T2 = T2.ID
FROM        T3
INNER JOIN  T1 ON T1.ID     = T3.ID_T1
INNER JOIN  T2 ON T2.ID_T1  = T1.ID

What you do is basically update ID_T2 with ID that comes from table T2 , which in turn is associated with T1 table with ID_T1 .

    
11.07.2018 / 18:58