×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Why the Aliases
4

Why the Aliases

Why the Aliases

(OP)
Hey Guys,

I was look at someone else's code and I don't understand why they would alias each field with [Extent1] when they aliased the table.

Or why would they add an AS on each field with the same name.

Any thoughts?

Thanks Simi


CODE --> sql

SELECT 
[Extent1].[Premise_Equipment_ID] AS [Premise_Equipment_ID], 
[Extent1].[Premise_Equipment_Type_ID] AS [Premise_Equipment_Type_ID], 
[Extent1].[Serial_Number] AS [Serial_Number], 
[Extent1].[MAC_ID] AS [MAC_ID], 
[Extent1].[IP_Address] AS [IP_Address], 
[Extent1].[Date_Installed] AS [Date_Installed], 
[Extent1].[Last_Inventory_Date] AS [Last_Inventory_Date], 
[Extent1].[Location_At_Premise] AS [Location_At_Premise], 
[Extent1].[Equipment_Status_ID] AS [Equipment_Status_ID], 
[Extent1].[Additional_Equipment_Notes] AS [Additional_Equipment_Notes], 
[Extent1].[Adtran_Information] AS [Adtran_Information], 
[Extent1].[Bridging_Instructions] AS [Bridging_Instructions], 
[Extent1].[Power_Supply_Information] AS [Power_Supply_Information], 
[Extent1].[Changed_Date] AS [Changed_Date], 
[Extent1].[Changed_User] AS [Changed_User]
FROM [dbo].[Premise_Equipment] AS [Extent1]
WHERE [Extent1].[Premise_Equipment_Type_ID] = @EntityKeyValue1 

RE: Why the Aliases

Typically when you see ugly code like that, it means it was created by a code generator such as Query by Example.

RE: Why the Aliases

2
Dave is right..probably built by a code generator.

One comment though...The table alias on each column.
My company's code standard is to ALWAYS alias a table AND to ALWAYS include the table alias in each column definition.
Why when there is only one table in the query?
Because tomorrow you will have to add one or more tables to the query.

Column aliases are used if/when needed/appropriate under our standards...

And it makes maintaining the code easier long term...IMHO.

CODE

SELECT
    ta.Id 'EmployeeId', --Alias needed for clarity
    ta.FirstName, --No alias needed, column name is self-documenting
    ta.LastName, --No alias needed, column name is self-documenting
    ta.FirstName + ' ' + ta.LastName 'FullName' --Alias needed for clarity
FROM TableA ta; 

Easily becomes

CODE

SELECT
    ta.Id 'EmployeeId', --Alias needed for clarity
    ta.FirstName, --No alias needed, column name is self-documenting
    ta.LastName, --No alias needed, column name is self-documenting
    ta.FirstName + ' ' + ta.LastName 'FullName' --Alias needed for clarity
    tb.Name 'DepartmentName' --Alias used for clarity
FROM TableA ta
INNER JOIN TableB tb
    ON ta.DeptId = tb.Id; 

Without the TableA alias in the first example, I would need to modify the entire query before adding in the TableB JOIN. Or at least I would need to modify ANY column that is named the same in the two tables and might create a processing error.

Robert "Wizard" Johnson III
U.S. Military Vets MC
Data Integration Engineer

RE: Why the Aliases

Star for SgtJarrow. Some great "standards" for writing SQL that is clear and sustainable.

My personal naming convention doesn't allow field names like "Id" and "Name". Also, every field in my databases has a unique name but I don't want to start a naming conventions war winky smile

Duane
Minnesota
Hook'D on Access
MS Access MVP 2001-2016

RE: Why the Aliases

We could argue all day on standards and naming conventions. Everyone has personal tastes and preferences.

The IMPORTANT things about standards are:
  • You have some standards
  • They make some sense
  • You ADHERE to them
For the most part, I (read as you) can adapt to any standard or convention. But you have to stick with it and use it correctly to reap the greatest benefits.

Robert "Wizard" Johnson III
U.S. Military Vets MC
Data Integration Engineer

RE: Why the Aliases

(OP)
Thanks all for the comments. I am think that Dave is correct.

I have seen a couple of other queries where the apparent same author aliased the table with [Extent1]. I thought it was a quirk of the author but it makes since that it would be a code generator.

Thanks

Simi

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close