T-SQL - Aliasing using "=" versus "as" [closed]

Solution 1:

‘=’ isn't valid ANSI SQL, so you'll have difficulty should you wish to run your application on a different DBMS.

(It's when ANSI form is used but the optional ‘AS’ is omitted I find the results difficult to read, personally.)

Solution 2:

To put in some counterweight, I prefer using =.

If I am the consumer of the query results in some way, I find it more convenient to see what columns I as a consumer can use.

I prefer this

SELECT
      [ElementObligationID] = @MaxElementObligationID + eo.ElementObligationID
      , [ElementID] = eo.ElementID
      , [IsotopeID] = eo.IsotopeID
      , [ObligationID] = eo.ObligationID
      , [ElementWeight] = eo.ElementWeight * -1
      , [FissileWeight] = eo.FissileWeight * -1
      , [Items] = eo.Items * -1
      , [Comment] = eo.Comment
      , [AdditionalComment] = eo.AdditionalComment
      , [Aanmaak_userid] = @UserID
      , [Aanmaak_tijdstip] = GetDate()
      , [Laatste_wijziging_userid] = @UserID
      , [Laatste_wijziging_tijdstip] = GetDate()
FROM  dbo.KTM_ElementObligation eo
      INNER JOIN dbo.KTM_ElementObligationArticle eoa ON 
          eoa.ElementObligationID = eo.ElementObligationID

over this

SELECT
      @MaxElementObligationID + eo.ElementObligationID AS [ElementObligationID]
      , eo.ElementID AS [ElementID]
      , eo.IsotopeID AS [IsotopeID]
      , eo.ObligationID AS [ObligationID]
      , eo.ElementWeight * -1 AS [ElementWeight]
      , eo.FissileWeight * -1 AS [FissileWeight]
      , eo.Items * -1 AS [Items]
      , eo.Comment AS [Comment]
      , eo.AdditionalComment AS [AdditionalComment]
      , @UserID AS [Aanmaak_userid]
      , GetDate() AS [Aanmaak_tijdstip]
      , @UserID AS [Laatste_wijziging_userid]
      , GetDate() AS [Laatste_wijziging_tijdstip]
FROM  dbo.KTM_ElementObligation eo
      INNER JOIN dbo.KTM_ElementObligationArticle eoa ON 
          eoa.ElementObligationID = eo.ElementObligationID

just my 2c.

Solution 3:

I wouldn't use it simply as it looks far too much like equality operation. 'AS' is clear inasmuch that it's not ambiguous to me.

Its the same as not using upper case in sql, I find it harder to read.

Solution 4:

I'm not as lucky as others who have posted here. The code I work with is usually written by someone else and it is rare that there are not CASE statements or other calculations, concatenations or logic that cause a single entry to span over several rows of T_SQL script.

Using a equal sign instead of 'AS' is by far easier to read. With an equal sign you know the alias name you are looking for is in the first position of the row. When 'AS' is used and the T_SQL spans multiple lines, the alias name could literally be anywhere.

It is far, far, far easier to find the 'Items' alias when equals is used than when 'AS' is used.

    SELECT
        ElementObligationID = @MaxElementObligationID + eo.ElementObligationID
      , ElementID = eo.ElementID
      , IsotopeID = eo.IsotopeID
      , ObligationID = eo.ObligationID
      , ElementWeight = eo.ElementWeight * -1
      , FissileWeight = eo.FissileWeight * -1
      , Items = CASE WHEN eo.Items < 0 THEN eo.Items * -1
                     WHEN eo.Items > 0 THEN eo.Items
                     ELSE 0 END
      , Comment = eo.Comment
      , AdditionalComment = eo.AdditionalComment
      , Aanmaak_userid = @UserID
      , Aanmaak_tijdstip = GetDate()
      , Laatste_wijziging_userid = @UserID
      , Laatste_wijziging_tijdstip = GetDate()
FROM  dbo.KTM_ElementObligation eo
      INNER JOIN dbo.KTM_ElementObligationArticle eoa ON 
          eoa.ElementObligationID = eo.ElementObligationID

Now imagine having more than 5 times the amount of code that is here and needing to find the 'Items' alias.

SELECT
      @MaxElementObligationID + eo.ElementObligationID AS ElementObligationID
      , eo.ElementID AS ElementID
      , eo.IsotopeID AS IsotopeID
      , eo.ObligationID AS ObligationID
      , eo.ElementWeight * -1 AS ElementWeight
      , eo.FissileWeight * -1 AS FissileWeight
      , CASE WHEN eo.Items < 0 THEN eo.Items * -1
             WHEN eo.Items > 0 THEN eo.Items
             ELSE 0 END AS Items
      , eo.Comment AS Comment
      , eo.AdditionalComment AS AdditionalComment
      , @UserID AS Aanmaak_userid
      , GetDate() AS Aanmaak_tijdstip
      , @UserID AS Laatste_wijziging_userid
      , GetDate() AS Laatste_wijziging_tijdstip
FROM  dbo.KTM_ElementObligation eo
      INNER JOIN dbo.KTM_ElementObligationArticle eoa ON 
          eoa.ElementObligationID = eo.ElementObligationID

'AS' vs '=' is not a capricious and arbitrary preference. I am not exaggerating when I say there have been times when it would take several minutes to find the alias name I am looking for because the author of the script I am now in charge of maintaining did not use the equals sign with their alias. I cannot think of a bigger waste of time, money and resources than paying an IT professional to look for alias names in code!! There is a right and wrong answer if you care about maintainability, readability, and efficiency. Your job is to provide business value, not to spend your day looking for Waldo!