Solved

Alias scope in SQL query

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Date Range Syntax Access 2003 10 54
sql calculate averages 18 47
SyBase Query Syntax Date Time conversion 4 27
Query Records that don't match 8 34
As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

911 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

15 Experts available now in Live!

Get 1:1 Help Now