Solved

Alias scope in SQL query

Posted on 2014-01-15
3
655 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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 tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

776 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