将SQL标准应用于脚本

why*_*heq 7 sql sql-server sql-standards

我有以下脚本,并希望对其进行更改,以使其符合国际标准.我使用SQL-Server,但只要有可能,我想遵循SQL的国际标准.我不相信方括号是标准的 - 我应该用双引号替换它们吗?

如果没有付费获得标准文档的副本,那么互联网上是否有任何资源可以提供按照标准要求格式化和布局的脚本示例?

SELECT 
    a.UserAccountKey,
    SUM(ISNULL(b.[measure Y],0.0)) AS "measure Y",
    SUM(ISNULL(c.[measure Z],0.0)) AS "measure Z"
FROM 
    XXX a
    LEFT OUTER JOIN YYYY b ON
        a.UserAccountKey = b.UserAccountKey
    LEFT OUTER JOIN ZZZZ c ON
        a.UserAccountKey = c.UserAccountKey
GROUP BY
    a.UserAccountKey
Run Code Online (Sandbox Code Playgroud)

编辑

我唯一不喜欢的经典标准是以下几点.这是由AaronBertrand提出的,我同意它更具可读性 - 特别是如果该SELECT条款有20或30个字段:

SELECT 
    a.UserAccountKey,
    "measure Y"             = SUM(ISNULL(b."measure Y",0.0)),
    "measure Z"             = SUM(ISNULL(c."measure Z",0.0)),
    "measure longertitle"   = SUM(ISNULL(c."measure longertitle",0.0)),
    "me short"              = SUM(ISNULL(c."me short",0.0))
FROM 
Run Code Online (Sandbox Code Playgroud)

Mar*_*ith 7

更改ISNULLCOALESCE和方括号",然后验证.

SELECT a.UserAccountKey,
       SUM(COALESCE(b."measure Y", 0.0)) AS "measure Y",
       SUM(COALESCE(c."measure Z", 0.0)) AS "measure Z"
FROM   XXX a
       LEFT OUTER JOIN YYYY b
         ON a.UserAccountKey = b.UserAccountKey
       LEFT OUTER JOIN ZZZZ c
         ON a.UserAccountKey = c.UserAccountKey
GROUP  BY a.UserAccountKey; 
Run Code Online (Sandbox Code Playgroud)

这意味着你需要确保QUOTED_IDENTIFIERON在SQL Server中.