Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Use the sp_describe_undeclared_parameters stored procedure to check if dynamic SQL has undeclared parametersSSMS - Enabling Ctrl+R Shortcut to show or hide query results/messages pane »
    comments

    What's wrong with the following code?

    1. SELECT
    2.     a.[BusinessEntityID]
    3.     , b.[FirstName]
    4.     , b.[LastName]
    5. FROM [HumanResources].[Employee] a
    6.     INNER JOIN [Person].[Person] b
    7.     ON b.[BusinessEntityID] = a.[BusinessEntityID]

    Nothing - except for my poor choice of using meaningless single characters as table aliases. Although it's not a big deal with simpler queries like I've here, it can be a maintenance nightmare with complex queries that join multiple tables.

    What about now? Is there anything wrong still?

    1. SELECT
    2.     e.[BusinessEntityID]
    3.     , p.[FirstName]
    4.     , p.[LastName]
    5. FROM [HumanResources].[Employee] e
    6.     INNER JOIN [Person].[Person] p
    7.     ON e.[BusinessEntityID] = p.[BusinessEntityID]

    No. This time I use e and p as aliases for Employee and Person respectively. Smart choice!

    But I notice a problem in team environments. Different developers use different aliases for the same table resulting in confusion and inconsistency.

    For example, some other developer might choose emp and ps instead of e and p like below.

    1. SELECT
    2.     emp.[BusinessEntityID]
    3.     , ps.[FirstName]
    4.     , ps.[LastName]
    5. FROM [HumanResources].[Employee] emp
    6.     INNER JOIN [Person].[Person] ps
    7.     ON emp.[BusinessEntityID] = ps.[BusinessEntityID]

    Solution:

    I use extended properties - following is an example script.

    1. EXEC sys.sp_addextendedproperty
    2. @name = N'TableAlias',
    3. @value = N'emp',
    4. @level0type = N'SCHEMA', @level0name = HumanResources,
    5. @level1type = N'TABLE',  @level1name = Employee ;
    6. GO
    7.  
    8. EXEC sys.sp_addextendedproperty
    9. @name = N'TableAlias',
    10. @value = N'per',
    11. @level0type = N'SCHEMA', @level0name = Person,
    12. @level1type = N'TABLE',  @level1name = Person ;
    13. GO

    Make no mistake, developers are still free to use different aliases, but it is at least easy to quickly see the standard alias by executing either of the following queries.

    1. SELECT [Schema] = s.NAME
    2.     , [Table] = t.NAME
    3.     , [Alias] = ep.value
    4. FROM sys.tables t
    5. INNER JOIN sys.schemas s ON s.schema_id = t.schema_id
    6. LEFT OUTER JOIN sys.extended_properties ep ON ep.major_id = t.object_id
    7.     AND ep.NAME = 'TableAlias' ;
    8.  
    9. SELECT *
    10. FROM fn_listextendedproperty('TableAlias', 'schema', 'Person', 'table', 'Address', NULL, NULL)

    Now I've to give a shout out to RedGate's SQL Promt. In addition to other features, SQL Prompt allows you to automatically assign table aliases, and specify custom aliases forcing you to use standard aliases.

    About the Author

    Samuel Vanga is a SQL Server Data Warehouse and Business Intelligence developer, and a LessThanDot.com blogger. Sam specializes in data integration using SQL Server Integration Services and building end-to-end business intelligence solutions. He also holds a MS degree in Computer Science.
    Social SitingsTwitterLinkedInLTD RSS Feed
    2863 views
    InstapaperVote on HN

    No feedback yet

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)