1
I have a table T1
where I have the ID
.
I have a table T2
where you take the ID_T1
(Foreign Key) of T1
.
I have a table T3
where you take the ID_T1
(Foreign Key) of T1
and the ID_T2
(Foreign Key) of T2
.
On the table T3
, the Ids of T1
are set, however, to T3
I created after these records were entered, IE, are with the column records ID_T2
to NULL
. Is there any way I can make some UPDATE
in T3
, passing all the Ids of the T2
in the right way?
Below is the code I’m trying:
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.
If the goal is just to update the ID_T2 table T3, just remove the WHERE, I think this is how it works.
– João Martins
@Joãomartins So dude, apart from WHERE, he changes all the fields to the same ID, that’s not what I’m looking for.
– Gabriel Henrique
Right, you’re absolutely right. New answer with code below.
– João Martins