SQL Server 2005 The multi-part identifier "tblPrefix.columnName" could not be bound.
We just went into Production with MSSQL 2005, and even though I knew the change of interpreter where Union / Order by is processed a little different, i thought the error thrown would make sense. For example, the below query in 2000 will run fine, but in 2005 will Error.
The known interpreter change gives a semi-cryptic error of "The multi-part identifier "tblPrefix.columnName" could not be bound." This basically means that in the order by b.bCol doesn't exists in both union queries. Therefore, the order by needs to drop the "b." becoming "bCol".
Solution:
select
a.aCol,
b.bCol
from a inner join b on a.id = b.id
union
select
a.acol,
c.bcol
form a inner join c on a.id = c.id
order by b.bCol
a.aCol,
b.bCol
from a inner join b on a.id = b.id
union
select
a.acol,
c.bcol
form a inner join c on a.id = c.id
order by b.bCol
The known interpreter change gives a semi-cryptic error of "The multi-part identifier "tblPrefix.columnName" could not be bound." This basically means that in the order by b.bCol doesn't exists in both union queries. Therefore, the order by needs to drop the "b." becoming "bCol".
Solution:
select
a.aCol,
b.bCol
from a inner join b on a.id = b.id
union
select
a.acol,
c.bcol
form a inner join c on a.id = c.id
order by b.bCol
a.aCol,
b.bCol
from a inner join b on a.id = b.id
union
select
a.acol,
c.bcol
form a inner join c on a.id = c.id
order by b.bCol
Comments
Post a Comment