Unix Technical Forum

SEO

vBulletin Search Engine Optimization


Go Back   Unix Technical Forum > Database Server Software > DB2

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 07:36 AM
Mark A
 
Posts: n/a
Default Variables in SP do not compare as equal when both are NULL

DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP
11).

Using the SAMPLE database, tables EMP and EMLOYEE.

In the followng stored procedure, 2 NULL columns (COMM) are selected into 2
different SP variables and compared for equal. They are both NULL, but do
not compare as equal. When the Not NULL columns (SALARY) are compared, they
do compare as equal.

Is there a reason for this?

Statement terminator is @.

-- SET THE COMMISION TO NULL ON TWO SAMPLE TABLES FOR EMPNO '000010'

UPDATE EMP SET COMM = NULL WHERE EMPNO = '000010'@
UPDATE EMPLOYEE SET COMM = NULL WHERE EMPNO = '000010'@

--------------------------------------------------
DROP PROCEDURE TEST_SP@

CREATE PROCEDURE TEST_SP
(
OUT EMP_COMM DECIMAL(9,2),
OUT EMP_SALARY DECIMAL(9,2),
OUT EMPLOYEE_COMM DECIMAL(9,2),
OUT EMPLOYEE_SALARY DECIMAL(9,2),
OUT v_ERRMSG_1 VARCHAR(500),
OUT v_ERRMSG_2 VARCHAR(500)
)
SPECIFIC TEST_SP
RESULT SETS 0
MODIFIES SQL DATA
LANGUAGE SQL
BEGIN

---------------------------------------------
-- Declare variables
---------------------------------------------

DECLARE not_found CONDITION FOR SQLSTATE '02000';
DECLARE at_end SMALLINT DEFAULT 0;

DECLARE A_EMPNO CHAR(6);
DECLARE A_EMP_COMM DECIMAL(9,2);
DECLARE A_EMP_SALARY DECIMAL(9,2);


DECLARE B_EMPNO CHAR(6);
DECLARE B_EMPLOYEE_COMM DECIMAL(9,2);
DECLARE B_EMPLOYEE_SALARY DECIMAL(9,2);

---------------------------------------------
-- Declare exit handlers
---------------------------------------------

DECLARE EXIT HANDLER FOR SQLEXCEPTION
GET DIAGNOSTICS EXCEPTION 1 v_ERRMSG_1 = MESSAGE_TEXT;

DECLARE CONTINUE HANDLER FOR not_found
SET at_end = 1;

DECLARE EXIT HANDLER FOR SQLWARNING
GET DIAGNOSTICS EXCEPTION 1 v_ERRMSG_1 = MESSAGE_TEXT;

---------------------------------------------------------------
-- Begin processing logic
---------------------------------------------------------------

SELECT EMPNO, COMM, SALARY INTO A_EMPNO, A_EMP_COMM, A_EMP_SALARY
FROM DB2INST1.EMP
WHERE EMPNO = '000010';

SELECT EMPNO, COMM, SALARY INTO B_EMPNO, B_EMPLOYEE_COMM, B_EMPLOYEE_SALARY
FROM DB2INST1.EMPLOYEE
WHERE EMPNO = A_EMPNO;

IF A_EMP_COMM = B_EMPLOYEE_COMM THEN
SET V_ERRMSG_1 = 'NULLS COMPARE AS EQUAL';
ELSE
SET V_ERRMSG_1 = 'NULLS DO NOT COMPARE AS EQUAL';
END IF;

IF A_EMP_SALARY = B_EMPLOYEE_SALARY THEN
SET V_ERRMSG_2 = 'NON-NULLS COMPARE AS EQUAL';
ELSE
SET V_ERRMSG_2 = 'NON-NULLS DO NOT COMPARE AS EQUAL';
END IF;

SET EMP_COMM = A_EMP_COMM;
SET EMP_SALARY = A_EMP_SALARY;

SET EMPLOYEE_COMM = B_EMPLOYEE_COMM;
SET EMPLOYEE_SALARY = B_EMPLOYEE_SALARY;

END@

CALL TEST_SP (?,?,?,?,?,?)@

Value of output parameters
--------------------------
Parameter Name : EMP_COMM
Parameter Value : -

Parameter Name : EMP_SALARY
Parameter Value : 52750.00

Parameter Name : EMPLOYEE_COMM
Parameter Value : -

Parameter Name : EMPLOYEE_SALARY
Parameter Value : 52750.00

Parameter Name : V_ERRMSG_1
Parameter Value : NULLS DO NOT COMPARE AS EQUAL

Parameter Name : V_ERRMSG_2
Parameter Value : NON-NULLS COMPARE AS EQUAL

Return Status = 0




Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 07:36 AM
mike
 
Posts: n/a
Default Re: Variables in SP do not compare as equal when both are NULL

> In the followng stored procedure, 2 NULL columns (COMM) are selected into 2
> different SP variables and compared for equal. They are both NULL, but do
> not compare as equal. When the Not NULL columns (SALARY) are compared, they
> do compare as equal.
>
> Is there a reason for this?


Yep, that's the way it's supposed to be.

db2 "values (case when (nullif(0,0) = nullif(0,0)) then 1 else 0 end)"

1
-----------
0

1 record(s) selected.

db2 "values (case when (nullif(0,0) is null AND nullif(0,0) IS NULL)
then>

1
-----------
1

1 record(s) selected.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-27-2008, 07:36 AM
Mark A
 
Posts: n/a
Default Re: Variables in SP do not compare as equal when both are NULL

"mike" <_link98@yahoo.com> wrote in message
news:1152510153.657801.41110@h48g2000cwc.googlegro ups.com...
> Yep, that's the way it's supposed to be.
>
> db2 "values (case when (nullif(0,0) = nullif(0,0)) then 1 else 0 end)"
>
> 1
> -----------
> 0
>
> 1 record(s) selected.
>
> db2 "values (case when (nullif(0,0) is null AND nullif(0,0) IS NULL)
> then>
>
> 1
> -----------
> 1
>
> 1 record(s) selected.
>


Yes, I figured out that is how it works. Is there is reason for this?


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-27-2008, 07:37 AM
Jeroen van den Broek
 
Posts: n/a
Default Re: Variables in SP do not compare as equal when both are NULL


"Mark A" <nobody@nowhere.com> schreef in bericht
news:f_6dnZGABreldyzZnZ2dnUVZ_oSdnZ2d@comcast.com. ..
> "mike" <_link98@yahoo.com> wrote in message
> news:1152510153.657801.41110@h48g2000cwc.googlegro ups.com...
>> Yep, that's the way it's supposed to be.
>>
>> db2 "values (case when (nullif(0,0) = nullif(0,0)) then 1 else 0 end)"
>>
>> 1
>> -----------
>> 0
>>
>> 1 record(s) selected.
>>
>> db2 "values (case when (nullif(0,0) is null AND nullif(0,0) IS NULL)
>> then>
>>
>> 1
>> -----------
>> 1
>>
>> 1 record(s) selected.
>>

>
> Yes, I figured out that is how it works. Is there is reason for this?
>


Yes, there is.
It is in the definition of NULL's.
A NULL means the specific value is unknown, NULL in itself isn't a value
that can be matched against any other value.

--
Jeroen


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-27-2008, 07:37 AM
mike
 
Posts: n/a
Default Re: Variables in SP do not compare as equal when both are NULL

> Yes, I figured out that is how it works. Is there is reason for this?

It is because in SQL, NULL is not a value, but the absence of one.
The equality predicate compares two values, not absence of them,
and if either of the operands to the equality predicate are NULL
then the result is unknown.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 02-27-2008, 07:37 AM
Mark A
 
Posts: n/a
Default Re: Variables in SP do not compare as equal when both are NULL

"mike" <_link98@yahoo.com> wrote in message
news:1152514099.822155.301690@75g2000cwc.googlegro ups.com...
>
> It is because in SQL, NULL is not a value, but the absence of one.
> The equality predicate compares two values, not absence of them,
> and if either of the operands to the equality predicate are NULL
> then the result is unknown.
>


That sounds like a circular argument to me (as does the other one posted by
Jeroene).

You claim that null is not a value, but the phrase "null value" turns up
frequently, including in DB2 documentation. And there are exceptions, such
as when using the DISTINCT predicate, where "null values are considered
equal. So (COL1 is NOT DISTINCT from COL2) will be true if both columns
contain an equal non-null value and also when both columns are the null
value." [quote from iSeries DB2 Infocenter].

Obviously, someone thought that relational theory dictated that two nulls
are not equal, but I don't quite understand under what conditions that it
would be useful. It just seems we have to do a lot of extra coding to get
around this "rule".


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 02-27-2008, 07:37 AM
mike
 
Posts: n/a
Default Re: Variables in SP do not compare as equal when both are NULL

> ... It just seems we have to do a lot of extra coding to get
> around this "rule".



Consider using COALESCE with predicates that involve nullable columns
to avoid explicitly coding the extra null tests.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 02-27-2008, 07:37 AM
Brian Tkatch
 
Posts: n/a
Default Re: Variables in SP do not compare as equal when both are NULL

Mark A wrote:
> "mike" <_link98@yahoo.com> wrote in message
> news:1152514099.822155.301690@75g2000cwc.googlegro ups.com...
> >
> > It is because in SQL, NULL is not a value, but the absence of one.
> > The equality predicate compares two values, not absence of them,
> > and if either of the operands to the equality predicate are NULL
> > then the result is unknown.
> >

>
> That sounds like a circular argument to me (as does the other one posted by
> Jeroene).
>
> You claim that null is not a value, but the phrase "null value" turns up
> frequently, including in DB2 documentation. And there are exceptions, such
> as when using the DISTINCT predicate, where "null values are considered
> equal. So (COL1 is NOT DISTINCT from COL2) will be true if both columns
> contain an equal non-null value and also when both columns are the null
> value." [quote from iSeries DB2 Infocenter].
>
> Obviously, someone thought that relational theory dictated that two nulls
> are not equal, but I don't quite understand under what conditions that it
> would be useful. It just seems we have to do a lot of extra coding to get
> around this "rule".


It is a misunderstanding of what NULL is. NULL is on a different
"level" than other values, as it talks about the value. All values are
either known or unknown. That is, if it is known, the actual value can
be returned, if it is unknown, only a big question-mark can be
returned.

The second thing is, that a WHERE clause tests for a boolean value, and
so, a truth-table can be used to discern what it means. For example,
the AND truth-table:

(T)RUE/(F)ALSE

T+T=T
T+F=F
F+T=F
F+F=F

If NULL is added to any equation, we cannot answer any questions,
because since we don't know the value, its truth cannot be discerned.
Being neither TRUE nor FALSE, we must say we don't know, which is
database terminology is called NULL. Comparing NULL to NULL is
comparing to completely unknown values. There is no way anyone could
figure out how to compare them, so the result must be NULL as well.

Ultimately, to deal with NULLs, the special keyword IS is used.

As for why the documentation refers to it as a NULL value, it is
because it is differentiation between types of value, not of the values
themselves.

As for the INDEX, INDEXes are not part of the data, as much as they are
part of making the database work. So, implementation of NULL values on
an INDEX is up to the developers, and database implementations are not
consistent.

If you find NULL being more trouble than it is worth, perhaps NULL is
not appropriate for your scenario. Because if you use =, you are saying
you know what the value is. If there is no value, it should be a
zero-length string ('') not a NULL-value.

B.

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9 (permalink)  
Old 02-27-2008, 07:37 AM
Serge Rielau
 
Posts: n/a
Default Re: Variables in SP do not compare as equal when both are NULL

Mark A wrote:
> "mike" <_link98@yahoo.com> wrote in message
> news:1152514099.822155.301690@75g2000cwc.googlegro ups.com...
>> It is because in SQL, NULL is not a value, but the absence of one.
>> The equality predicate compares two values, not absence of them,
>> and if either of the operands to the equality predicate are NULL
>> then the result is unknown.
>>

>
> That sounds like a circular argument to me (as does the other one posted by
> Jeroene).

To make a long story short: ANSI SQL
There are ton's of papers on how NULL should or shouldn't behave.

> You claim that null is not a value, but the phrase "null value" turns up
> frequently, including in DB2 documentation. And there are exceptions, such
> as when using the DISTINCT predicate, where "null values are considered
> equal. So (COL1 is NOT DISTINCT from COL2) will be true if both columns
> contain an equal non-null value and also when both columns are the null
> value." [quote from iSeries DB2 Infocenter].

Correct. It's so special they had to talk about it. :-)
There is an alternative predicate in the works for the SQL Standard
which will treat NULL as equal because there are valid arguments for
both sides.

Cheers
Serge


--
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab

IOD Conference
http://www.ibm.com/software/data/ond...ness/conf2006/
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10 (permalink)  
Old 02-27-2008, 07:37 AM
Mark A
 
Posts: n/a
Default Re: Variables in SP do not compare as equal when both are NULL

"Brian Tkatch" <Maxwell_Smart@ThePentagon.com> wrote in message at
conditions that it
>
> If NULL is added to any equation, we cannot answer any questions,
> because since we don't know the value, its truth cannot be discerned.
> Being neither TRUE nor FALSE, we must say we don't know, which is
> database terminology is called NULL. Comparing NULL to NULL is
> comparing to completely unknown values. There is no way anyone could
> figure out how to compare them, so the result must be NULL as well.
>


If COMM on table EMP is null, and COMM on table EMPLOYEE is null (both for
the same EMPNO= '000010', which is the PK), if I say:

IF EMP.COMM = EMPLOYEE.COMM

then I "CAN" compare them and IMO, they should be equal even if both values
are null. I understand that you disagree, but that is your opinion.

> Ultimately, to deal with NULLs, the special keyword IS is used.


Not exactly. What I can do is:

IF COALESCE(EMP.COMM,0) = COALESCE(EMPLOYEE.COMM,0)

or

IF ((EMP.COMM = EMPLOYEE.COMM) OR (EMP.COMM IS NULL AND EMPLOYEE.COMM IS
NULL))

It seems to me that those who actually have a need for IF EMP.COMM =
EMPLOYEE.COMM to test false (assuming that both columns are null) are in the
small minority (it would be hard for me to imagine what that would be used
for) and that any special syntax for it test true (as Serge mentioned may be
coming) should have been reserved for the rare exceptions and not the
majority case where most people would expect it to test true.

> As for why the documentation refers to it as a NULL value, it is
> because it is differentiation between types of value, not of the values
> themselves.


This sounds like gobblygook to me. If the column is null, then it contains
null values. The value is null.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 07:45 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
UnixAdminTalk.com

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562