When i try to make a sub-query with a left join
i got an error here's an example.
Select * FROM ( SELECT Code,Column2 FROM T1) TSubQuery, Table2 T2
Left join Table3 T3
On T3.Code = TSubQuery.Code -- <== Error triggered
Where TSubQuery.Code= T2.Code
this code will triggered the following error :
Msg 4104, Level 16, State 1, Line 3 The multi-part identifier "TSubQuery.Code" could not be bound.
So i solved this problem by changing my Where
into a Inner join
clause.
Select * FROM ( SELECT Code,Column2 FROM T1) TSubQuery
Left join Table3 T3
On T3.Code = TSubQuery.Code
Inner join Table2 T2
On T2.Code = TSubQuery.Code
I would like to understand, why I have this error ?
there's an other way to solve it without using an inner join
or with a with clause
?
why I don't have this problem when am I using a normal table or with a with table
?
Example:
With TQuery as ( SELECT * FROM T1)
select * from TQuery, Table2 T2
Left join Table3 T3
On T3.Code = TQuery.Code
Where TQuery.Code = T2.Code