Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Alias scope in SQL query

Posted on 2014-01-15
3
Medium Priority
?
783 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
[X]
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
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 71

Accepted Solution

by:
Qlemo earned 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

636 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