Domanda

I have the following script and would like to change it so that it agrees with the international standards. I use SQL-Server but whenever possible I'd like to follow the international standards for SQL. I don't believe that the square brackets are standard - should I replace them with double quotes?

Without paying to get a copy of the standards document are there any resources on the internet which give examples of scripts formatted and laid out exactly as required by the standards?

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

EDIT

My only slight preference that is not classic standard is the following. This was put forward by AaronBertrand and I agree that it's more readable - especially if the SELECT clause has 20 or 30 fields:

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 
È stato utile?

Soluzione

Change ISNULL to COALESCE and square brackets to " and then it validates.

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; 

This does mean that you need to ensure that QUOTED_IDENTIFIER is ON in SQL Server.

Altri suggerimenti

A good online free T-SQL formatter is http://www.tsqltidy.com/, i.e. for SQL Server. Careful as your firewall admin might suddenly be alerted about an SQL Injection attack (some firewalls mistake using the site for an attack). Otherwise there are commercial tools that have formatting capabilities - SQL Complete (DevArt) and SQL Prompt (Red Gate).

As for "International Standard SQL", that would be more of a blog-site of it's own. Which ANSI Standard (1992, SQL3, ...) and to what level ?

Can help you with the quotes though, use SET QUOTED_IDENTIFIER ON; before your SQL, and then turn off afterwards (SET QUOTED_IDENTIFIER OFF;). That means you don't need to switch it on for the whole database. But, it's a good idea NOT use identifiers with spaces and non-standard characters (just as C# with code like "... new Object type I just made up ~() ... " - would not be very practical).

Does that count as 2 answers and another question ?

Here is a good list of resources for SQL standards with different documents and everything: http://gerardnico.com/wiki/language/sql/ansi

If you’re looking for a t-SQL formatter for your existing code you can also check out ApexSQL Refactor . It’s a free SSMS plugin similar to SQL Promt from Red Gate.

Another great resource is SQL Cop, which is a free tool that identifies issues and suggests best practices:

http://sqlcop.lessthandot.com/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top