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

2 thoughts on “We Are TOTALLY Amused: A T-SQL Error

  1. Meredith Ryan-Smith

    Years ago we had an application that didn’t provide for a way to reset a users password through the admin tools. If someone forgot their password and needed it reset we had to update the table manually. There was a certain set of conditions that would generate the following error in SQL EM when we updated the password value:

    ‘Transaction cannot start wile in firehose mode’

    that is by far my favorite error message ever 🙂

  2. Pingback: Totally amused | Mendocinophoto

Comments are closed.