代码之家  ›  专栏  ›  技术社区  ›  mwjackson

T-SQL-别名使用“=”与“as”

  •  16
  • mwjackson  · 技术社区  · 15 年前

    当对列进行别名时,是否有任何特殊的原因(性能或其他方面)使用before=

    我个人的偏好(可读性)是使用:

    select
    alias1     = somecolumn
    alias2     = anothercolumn
    from
    tables
    etc...
    

    而不是这个:

    select
    somecolumn as alias1
    anothercolumn as alias2
    from
    tables
    etc...
    

    我有什么理由不该这样做吗?在格式化列时,其他人的首选项是什么?

    15 回复  |  直到 15 年前
        1
  •  17
  •   bobince    15 年前

        2
  •  13
  •   Lieven Keersmaekers    15 年前

    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
    

    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
    

        3
  •  8
  •   Preet Sangha    15 年前

        4
  •  4
  •   Marc Gravell    15 年前

    somecolumn as 'alias 1'
    

    'alias 1' = somecolumn
    

    somecolumn as [alias 1]
    
        5
  •  4
  •   gbn    15 年前

    select
        alias1     = somecolumn,
        alias2     = anothercolumn,
        result     = column1 * column2
    from
        table
    ....
    
    
    select
        somecolumn as          alias1,
        anothercolumn as       alias2,
        column1 * column2 as   result
    from
        tables
         ...
    
        6
  •  4
  •   mihalko    7 年前

        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
    

    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
    

        7
  •  3
  •   Damien_The_Unbeliever    15 年前

    select
        columnA,
        columnB
        columnC
    from
        table
    

    select
        cA = columnA,
        @cB = columnB,
        cC = columnC
    from
        table
    
        8
  •  3
  •   D'Arcy Rittich    15 年前

    AS

    ON CASE

        9
  •  2
  •   pgb    15 年前

    AS =

        10
  •  2
  •   Scoregraphic    15 年前

    SELECT MAX(price_column) maximumprice FROM prices
    
        11
  •  2
  •   jerryhung    15 年前

    SELECT
     column1 = table.column1
     ,column2 = table.colum2
    FROM table
    

        12
  •  1
  •   devio    15 年前
        13
  •  1
  •   HLGEM    15 年前

        14
  •  0
  •   Johnno Nolan    15 年前

    SELECT originalname alias
    FROM
       tablename
    
        15
  •  -1
  •   anishMarokey FIre Panda    15 年前

     column as alias1