4

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
Hicham Bouchilkhi
  • 682
  • 10
  • 29

5 Answers5

1

At first as I say in the comment before, do not use Old-Style joins. What causing the error is that you have the same column name in both table (Code column in your case), and SQL Server will don't know which one you refer too, like you say to me "Esperadoce" and I say which "Esperadoce" you refer to?.

What would be the solution for that?

The solution is using Aliases as:

SELECT T1.Code AS T1Code, T2.Code AS T2Code From ...

Also you have a Mixed query with Old and modern style joins, So solution would be:

  • Use Columns names with Aliases instead of SELECT *.

  • Use Modern style joins instead of the old style, and do not mix them.

Community
  • 1
  • 1
Ilyes
  • 14,640
  • 4
  • 29
  • 55
1

Implicit JOIN is the problem here, see the line below

 FROM ( SELECT Code,Column2 FROM T1) TSubQuery, Table2 T2

and pay attention to

TSubQuery, Table2

remove that coma and move table2 to a explicit join. You are mixing the old "deprecated" syntax with "modern" explicit join.

Deprecation of "Old Style" JOIN Syntax: Only A Partial Thing

jean
  • 4,159
  • 4
  • 31
  • 52
1

Don't use the where clause for join conditions. Do this, working fiddle

SELECT
      *
  FROM
      (
        SELECT
              [Code],
              [Column2]
          FROM
              [dbo].[T1]
      ) TSubQuery
    JOIN
      [dbo].[Table2] T2
        ON TSubQuery.[Code] = T2.[Code]
    LEFT JOIN
      [dbo].[Table3] T3
        ON T3.[Code] = TSubQuery.[Code]

The legacy join syntax is obsolete and provided only for backward compatibility. And whats more, it sucks.

Additionally, for what its worth, don't use SELECT *.

Jodrell
  • 34,946
  • 5
  • 87
  • 124
0

Try this

with TSubQuery
as
(
    SELECT Code,Column2 FROM T1
)
Select 
    * 
    FROM TSubQuery
        left join Table2 T2
            on TSubQuery.Code= T2.Code
        Left join Table3 T3
            On T3.Code = TSubQuery.Code
Jayasurya Satheesh
  • 7,826
  • 3
  • 22
  • 39
0

the subquery can be avoided in this way:

SELECT Code,Column2 FROM T1 as TSubQuery
Left join Table3 T3
On T3.Code = TSubQuery.Code 
Inner join  Table2 T2
On T2.Code = TSubQuery.Code 
Jayvee
  • 10,670
  • 3
  • 29
  • 40