Solved

Alias scope in SQL query

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

Expert Comment

by:awking00
Comment Utility
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 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
Comment Utility
Thank you to both of you for answering.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VB Access SQL question 2 22
Access Join Query 12 47
monitor queries that use too much tempdb log 20 34
SQL Help joining two tables 7 33
In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now