Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Using alias name is a good approach??

Author  Topic 

pearlkin
Starting Member

3 Posts

Posted - 2011-05-30 : 08:02:18
Using alias name is a good approach??

16 minutes ago

Hi

Which is the best approach and Tell Why??

when we try to get data from more than 1 table by using joins. In Query we will prefix like a.ColumnName or tableName.ColumnName

for eg

1)Select a.id, a.name, a.address, b.salary,b.role from details as a inner join empdetails as b on a.id=b.id

or

2)Select details.id, details.name, details.address,empdetails.salary,empdetails.role from details inner join empdetails on details.id=empdetails.id

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2011-05-30 : 08:20:47
There's two general schools of thought

1) Alias everything and be verbose (me) - It makes it easier for the next guy to understand what you are doing.
2) Alias nothing except what you need to. (I don't like this but I do understand that some people use this because it makes it harder to get at 'accidentally correct query').

I'd rewrite your query as:

SELECT
d.[Id] AS [DetailsId]
, d.[Name] AS [DetailsName]
, d.[Address] AS [DetailsAddress]
, ed.[salary] AS [EmployeeSalary]
, ed.[role] AS [EmployeeRole]
FROM
details AS d
JOIN empdetails AS ed ON ed.[Id] = d.[Id]


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION


EDIT - I'm *extremely* verbose but that's just me. I have a bit of a problem and like my SQL to be 'nice'
Go to Top of Page
   

- Advertisement -