views:

294

answers:

4

Hello Does anyone know why the following query fails in Derby?

delete from MyTable
where
((null = null) or (col1 = null)) OR
((102 = null) or (col2 = 102)))

I get the following error:

Error: Syntax error: Encountered "null" at line 3, column 3.
SQLState:  42X01
ErrorCode: -1

The sql is being generated in a java program based on SQL I've written into an iBatis ORM layer config. The prepared statement is as follows:

delete from MyTable
where
((? = null) or (col1 = ?)) OR
((? = null) or (col2 = ?)))

As you can see what I'm trying to say is that if the parameter is not null then test the column value against the parameter

A: 

You do not write SQL like that. Not sure how it is for Derby but for most it is:

delete from MyTable 
where 
((col1 IS null)) OR 
((102 IS null) or (col2 = 102))) 
Oskar Kjellin
+3  A: 

I believe what you want is:

DELETE FROM `name_of_table` WHERE `name_of_column` IS NULL

SQL has this really weird property where you can use =value with most values, except NULL.

Michael Aaron Safyan
Nothing weird. NULL != NULL is core part of trinary logic. If soething is not known (NULL) it is not identical to some other not known value.
TomTom
@TomTom, what is weird is that SQL uses "IS" instead of "=" only for the value of NULL.
Michael Aaron Safyan
Actually no. tIt is the logical conclusion of trinary logic.
TomTom
@TomTom, it is neither logical nor illogical... it is all a matter of the meaning that we choose to assign to syntax. In my own opinion, it is weird that SQL special cases NULL in this way, because the intended meaning of such a statement is obvious and unambiguous, and most other languages with a notion of null do not require a special syntax for determining if something is null.
Michael Aaron Safyan
+1  A: 

The first part of the where clause suggests it should match every row (if you expect null = null to be true, which it generally wouldn't be in SQL) - do you really want to delete the whole table's contents? Likewise 102 = null is a pretty bizarre kind of comparison, unless you've somehow got a column called 102 (which I'd at least hope is prohibited).

What are you trying to actually match? As others have said, you should use "is null" to compare a value against null - but it sounds like your query has rather bigger problems. If you could tell us what you're trying to achieve, we may be able to help you write the query better.

Jon Skeet
The query is a parameterised query and what I'm trying to say is if the *parameter* is null then ignore the condition. I've edited the question to clarify this *hopefully*.
leftbrainlogic
@leftbrainlogic: Are the first and third parameters meant to be column names or values? If they're values, you can check for nullity yourself and adjust the query accordingly rather than passing the data to SQL.
Jon Skeet
+2  A: 

In SQL, any operator where one of the arguments is NULL evaluates to NULL. So 1 + NULL is NULL, MAX(NULL) is NULL, and so on. Most importantly, in your case, X = NULL always evaluates to NULL, even if X is NULL itself (that is, NULL = NULL evaluates to NULL not true).

The exact error you get is that the keyword NULL is not actually valid on the left-hand side of a comparison. But even if you fixed that, you'd still find that none of your rows ever match. As others have said, the correct comparison to use when trying to determine whether something is null is IS NULL, as in X IS NULL.

Dean Harding