Earlier Firebird versions
Pre-2.0 versions of Firebird don’t support this use of DISTINCT
.Consequently, the tests are a little more complicated and there are some pitfalls to avoid.
The correct equality test for pre-2.0 Firebird versions is:
if (A = B or A is null and B is null) then...
or, if you want to make the precedence of the operations explicit:
if ((A = B) or (A is null and B is null)) then...
A word of warning though: if exactly one of A
and B
is NULL
, the test expression becomes NULL
, not false
!This is OK in an if
statement, and we can even add an else
clause which will be executed if A
and B
are not equal (including when one is NULL
and the other isn’t):
if (A = B or A is null and B is null)
then ...stuff to be done if A equals B...
else ...stuff to be done if A and B are different...
But don’t get the bright idea of inverting the expression and using it as an inequality test:
/* Don't do this! */
if (not(A = B or A is null and B is null))
then ...stuff to be done if A differs from B...
The above code will work correctly if A
and B
are both NULL
or both non-NULL
.But it will fail to execute the then
clause if exactly one of them is NULL
.
If you only want something to be done if A
and B
are different, either use one of the correct expressions shown above and put a dummy statement in the then
clause (starting at 1.5, an empty begin … end
block is also allowed), or use this longer test expression:
/* This is a correct inequality test for pre-2 Firebird: */
if (A <> B
or A is null and B is not null
or A is not null and B is null) then...
Remember, all this is only necessary in pre-2.0 Firebird versions.From version 2 onward, the inequality test is simply “if (A is distinct from B)
”.