Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Alias scope in SQL query

Posted on 2014-01-15
3
664 Views
Last Modified: 2014-01-16
Why is it that once an alias is declared for the table that the fully qualified name can no longer be used? I find this curious and am just interested if anyone has any insight into why this is or if this is just T-SQL.

Seems like a scope issue, that once an alias is declared below for the tables (i and p) that the fully qualified names are no longer valid.

Thanks.

USE AdventureWorks2012;
GO
SELECT i.SalesOrderDetailID
FROM SalesLT.SalesOrderDetail AS i
INNER JOIN SalesLT.SalesOrderHeader AS p
ON SalesLT.SalesOrderDetail.SalesOrderID = SalesLT.SalesOrderDetail.SalesOrderID

results in an error

but
USE AdventureWorks2012;
GO
SELECT SalesOrderDetailID
FROM SalesLT.SalesOrderDetail
INNER JOIN SalesLT.SalesOrderHeader
ON SalesLT.SalesOrderDetail.SalesOrderID = SalesLT.SalesOrderDetail.SalesOrderID

is fine
and this is also fine

USE AdventureWorks2012;
GO
SELECT i.SalesOrderDetailID
FROM SalesLT.SalesOrderDetail AS i
INNER JOIN SalesLT.SalesOrderHeader AS p
ON i.SalesOrderID = p.SalesOrderID
0
Comment
Question by:alexmac05
3 Comments
 
LVL 32

Expert Comment

by:awking00
ID: 39782924
One of the primary reasons for using an alias is so you don't have to use the fully qualified name and why would you create an alias if you're not going to use it?
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 39783047
This is a general "feature" of all SQL dialects I know of, and not restricted to T-SQL. Once you introduce an alias, you can't use the real name. That is because you rename the internal object name. It isn't just an alias, it is the name.
0
 

Author Comment

by:alexmac05
ID: 39785670
Thank you to both of you for answering.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question