What is the proper syntax in SQL Server for addressing tables?

前端 未结 7 441
-上瘾入骨i
-上瘾入骨i 2021-01-18 10:05

This seems like a fairly obvious question, but I haven\'t been able to think of the proper term for what I am trying to ask, so coming up with reference material for this ha

相关标签:
7条回答
  • 2021-01-18 10:30

    I think this is one of those questions that's subjective, and will turn out to be a matter of preference, but:

    From a readability perspective, I'd argue for being explicit ONLY when necessary. Quite often SQL statements are complex enough without having to read a lot of unnecessary text.

    I think that

    SELECT TOP 1000 [StoreNumber]
          ,[Address1]
          ,[Address2]
          ,[City]
          ,[St]
          ,[Zip]
          ,[ZipSuffix]
          ,[LocationType]
          ,[LocationSubType]
          ,[Corp]
          ,[Division]
          ,[ZoneNumber]
          ,[DistrictNumber]
          ,[StateNumber] 
      FROM [CommonData].[dbo].[vw_StoreData]
    

    is a LOT more readable than

    SELECT TOP 1000 [CommonData].[dbo].[vw_StoreData].[StoreNumber]
          ,[CommonData].[dbo].[vw_StoreData].[[Address1]
          ,[CommonData].[dbo].[vw_StoreData].[[Address2]
          ,[CommonData].[dbo].[vw_StoreData].[[City]
          ,[CommonData].[dbo].[vw_StoreData].[[St]
          ,[CommonData].[dbo].[vw_StoreData].[[Zip]
          ,[CommonData].[dbo].[vw_StoreData].[[ZipSuffix]
          ,[CommonData].[dbo].[vw_StoreData].[[LocationType]
          ,[CommonData].[dbo].[vw_StoreData].[[LocationSubType]
          ,[CommonData].[dbo].[vw_StoreData].[[Corp]
          ,[CommonData].[dbo].[vw_StoreData].[[Division]
          ,[CommonData].[dbo].[vw_StoreData].[[ZoneNumber]
          ,[CommonData].[dbo].[vw_StoreData].[[DistrictNumber]
          ,[CommonData].[dbo].[vw_StoreData].[[StateNumber] 
      FROM [CommonData].[dbo].[vw_StoreData]
    

    (It gets worse when you start joining tables, and even worse if you're joining tables across different databases.)

    I can see where you could argue that the second is more readable if you need to know exactly which database, schema, and table a particular field comes from by looking at the query alone.

    But in SQL Server, for example, you'd be able to open that query in a designer and see it in a much more friendly graphical view.

    IMHO, the only time that I would use the full syntax is when necessary, when crossing table/database/schema boundaries, or if you've got two tables with the same field name.

    Example:

    SELECT TOP 1000 [CommonData].[dbo].[vw_StoreData].[StoreNumber]
          ,[Address1]
          ,[Address2]
          ,[City]
          ,[St]
          ,[Zip]
          ,[ZipSuffix]
          ,[LocationType]
          ,[LocationSubType]
          ,[Corp]
          ,[Division]
          ,[ZoneNumber]
          ,[DistrictNumber]
          ,[StateNumber] 
      FROM [CommonData].[dbo].[vw_StoreData]
      Inner Join [CommonData].[dbo].[vw_StorePhones]
       ON [CommonData].[dbo].[vw_StorePhones].[StoreNumber] = [CommonData].[dbo].[vw_StoreData].[StoreNumber]
    

    And even in that case, I'd use Table Aliases to shorten it up and make it more readable.

    All of that said, in the real world, it's quite likely you'll find yourself working for a company that has already decided the standard format, and you'll need to code according to the company's standard.

    0 讨论(0)
提交回复
热议问题