Blog Post

We Are TOTALLY Amused: A T-SQL Error

,

This has to be the best error I’ve ever seen:

Msg 195, Level 15, State 10, Line 2
‘SUM’ is not a recognized built-in function name.

Uhm, I’m sorry to contradict you SQL Server, but yes it is. Here’s the code that produced that error:

SELECT keyID

, SUM(CASE WHEN printDate IS NULL THEN 1 ELSE 0 END AS printed)

, COUNT(*) total

FROM MyTable

GROUP BY keyID

The problem with this particular piece of code is, of course, in that SUM line…but it’s not the SUM that’s the problem.  SQL didn’t know what to make of the misplaced column alias “AS printed”, which should go outside the parentheses:

SELECT keyID

, SUM(CASE WHEN printDate IS NULL THEN 1 ELSE 0 END) AS printed

, COUNT(*) total

FROM MyTable

GROUP BY keyID

I don’t have a point, especially, except to say that errors can be somewhat misleading!

Happy days,

Jen McCown
http://www.MidnightDBA.com/Jen

Rate

You rated this post out of 5. Change rating

Share

Share

Rate

You rated this post out of 5. Change rating