Search code examples
sql-servert-sqltable-alias

Why can't I use table names of aliased tables in WHERE clause?


Why won't SQL Server let me do this? It gives me 3

The multi-part identifier ... could not be bound.

errors because I'm using the actual table name, not the table alias, to reference columns (this is a simplified example).

SELECT TS_USERS.ts_id, ts_Users.ts_name  
FROM ts_Users u  
WHERE ts_users.ts_id > 0;

of course this works

SELECT u.ts_id, u.ts_name  
FROM ts_Users u  
WHERE u.ts_id > 0;

It's like SQL Server forgets the actual table name once that table is aliased (within the context of the SELECT)?


Solution

  • By using an alias [like u], you are telling SQL that within the context of this query, henceforth only refer to the table as u and only u.

    If you have a way to turn off the alias when generating that command, try that. Or else try making the alias the same as the tablename.