Wednesday, December 11, 2024

The multi-part identifier XXX could not be bound in SQL SERVER [Solution]

Problem, I was doing join statement and getting this error Msg 4104, Level 16, State 1, Line 69

The multi-part identifier "a.X" could not be bound."


Cause:

I was mixing implicit joins with explicit joins. That is allowed, but you need to be aware of how to do that properly.

The thing is, explicit joins (the ones that are implemented using the JOIN keyword) take precedence over implicit ones (the 'comma' joins, where the join condition is specified in the WHERE clause).


Here's an outline of your query:

SELECT

a.*

FROM a, b LEFT JOIN dkcd ON a

WHERE a

You are probably expecting it to behave like this:


SELECT

a

FROM (a, b) LEFT JOIN dkcd ON a

WHERE a

that is, the combination of tables a and b is joined with the table dkcd. In fact, what's happening is


SELECT

a

FROM a, (b LEFT JOIN dkcd ON a)

WHERE a

that is, as you may already have understood, dkcd is joined specifically against b and only b, then the result of the join is combined with a and filtered further with the WHERE clause. In this case, any reference to a in the ON clause is invalid, a is unknown at that point. That is why you are getting the error message.

This can also come when you are using different alias for same table at different place e.g different alias on JOIN clause and different alias on WHERE clause


Solution: 

Rewrite the SQL Query to use either explicit join or rearrange the order so that the column is available. 


    No comments :

    Post a Comment