Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: SQL Server
Expertise: Beginner
Jul 25, 2000

Selecting Data from Views with Aliases

Question:
If I have created a view from two tables that happen to have a similar column name (e.g., name) :
CREATE VIEW team AS SELECT p.name, p.age, t.name, t.city 
FROM PLAYER p, TEAM t where ...
How do I then select this information from the view? Do I have to use the same aliases?

Answer:
The aliases of "p" and "t" are only for the table names. To create a view that includes columns of the same name, you need to create an alias for the column, like this:

CREATE VIEW team AS SELECT p.name AS PName, p.age AS PAge, 
t.name AS TName, t.city AS TCity FROM PLAYER p, TEAM t
where ....
Then you access the columns by their aliases, like this:
SELECT PName, PAge, TName
FROM Team
If you try to create a view with columns of the same name, it will return an error.
DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date