Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to ignore SQL table alias?

Is it in anyway possible to reference a strongly typed SQL table object directly, even after it has been aliased?

For example, the following script renders a The multi-part identifier "dbo.MyTable.Col3" could not be bound exception:

SELECT
    *
FROM dbo.MyTable MT
    INNER JOIN dbo.AnotherTable AT ON
        MT.Col1 = AT.Col2
WHERE
    dbo.MyTable.Col3 = 'Foo'
like image 324
Rudolf Lamprecht Avatar asked Aug 11 '16 08:08

Rudolf Lamprecht


People also ask

Do you need AS for alias in SQL?

Should You Use the SQL AS Keyword with Table Aliases? The AS keyword is optional when using it with a table alias.

WHERE can you not use aliases in SQL?

Standard SQL disallows references to column aliases in a WHERE clause. This restriction is imposed because when the WHERE clause is evaluated, the column value may not yet have been determined.

Why do we need to create a table alias?

TABLE ALIASES are used to shorten your SQL to make it easier to read or when you are performing a self join (ie: listing the same table more than once in the FROM clause).

How does table alias names affect the performance of the database?

An alias removes ambiguity when you have more than one table because you know which table it comes from. It also prevents future table changes from breaking a query.


1 Answers

No, you cannot.

To quote from FROM (Transact-SQL):

The table name cannot be used if an alias is defined.

like image 162
Damien_The_Unbeliever Avatar answered Sep 16 '22 18:09

Damien_The_Unbeliever