Wednesday, January 17, 2007

 

None of the result expressions in a CASE specification can be NULL - Error!

The other day I got a question from a former student about a strange CASE statement error message she was getting: "None of the result expressions in a CASE specification can be NULL". She was kind enough to post her code and, sure enough, when I tested it, I got the same error. Now I've been around SQL Server for a long time but I've never seen SQL Server throw error 8133 so I started playing around... First - let's have a look at the code:
USE tempdb
GO
CREATE TABLE MyTable (EmpId INT NOT NULL PRIMARY KEY, EmpName VARCHAR(128))
GO
INSERT MyTable VALUES (1, 'Scott Whigham')
INSERT MyTable VALUES (2, 'Aimee Wilson')

SELECT EmpId, CASE WHEN EmpName = 'Scott Whigham' THEN NULL END AS TheName FROM MyTable
When (or "if") you try to run this code, you too will get error 8133, "None of the result expressions in a CASE specification can be NULL". If you know anything about SQL Server's case statements, you know that to be a lie - and we can easily prove it:
SELECT EmpId, CASE WHEN EmpName = 'Scott Whigham' THEN NULL ELSE EmpName END AS TheName FROM MyTable
Simply by adding the ELSE clause into the statement we get the query to run without an error - therefore, as far as I can tell, the error text for 8133 is misleading at best.

Here's a funny twist on it: if you reverse the logic of the original, offending statement, it works great:
SELECT EmpId, CASE WHEN EmpName <> 'Scott Whigham' THEN EmpName END AS TheName FROM MyTable
Although the syntax is different, this query is the same as the first query listed above yet it works fine. If you aren't aware of the logic of the CASE operator, because there is no ELSE clause in the above query, then it returns NULL for any EmpName = 'Scott Whigham' (which was what the first query asked for explicitly).

I can't find anything on the web about this error that gives me a reason for it showing up here - anyone have any ideas?


Comments:
I think it might be a problem only when all paths in a CASE return NULL. When that's the case, there's no point in using a CASE statement, so maybe that's what the SQL Server developers were trying to alert people to.
 
Thanks for posting about this. I had this same error today. :)
 
"I think it might be a problem only when all paths in a CASE return NULL. When that's the case, there's no point in using a CASE statement, so maybe that's what the SQL Server developers were trying to alert people to."

I don't think so - how would you know at design time that what the data in a column that allows NULLs will always, 100% of the time return at least one non-null value?
 
It's because sql server doesn't know what the data type should be for the result of the case. At least one of the result expressions needs to be non-null.

SELECT EmpId, CASE WHEN EmpName = 'Scott Whigham' THEN null when empname = 'foo' then 'spork' END AS TheName FROM #MyTable
 
Post a Comment

Links to this post:

Create a Link



<< Home

This page is powered by Blogger. Isn't yours?