Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply SQL Standards to script

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 
like image 899
whytheq Avatar asked Feb 05 '13 10:02

whytheq


2 Answers

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.

like image 153
Martin Smith Avatar answered Oct 05 '22 14:10

Martin Smith


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 ?

like image 38
Grignar Grenac Avatar answered Oct 05 '22 13:10

Grignar Grenac