If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > SQL0801 - Division by zero was attempted

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-11-06, 22:18
dollar489 dollar489 is offline
Registered User
 
Join Date: Sep 2002
Posts: 456
SQL0801 - Division by zero was attempted

The following code in SP stopped working when database upgrade to FP11 happened:

IF(IPI_LOW_BRNG_TMP <> 0) THEN SET LD_TEMP_RATIO = (IPI_HIGH_BRNG_TMP * 1.0)/(IPI_LOW_BRNG_TMP * 1.0)

It worked fine on FP5. Any idea?

Dollar
Reply With Quote
  #2 (permalink)  
Old 09-12-06, 01:56
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
Hi,
if the code was working fine on fixpack 5 and after upgrade to fixpack 11 it doesn't work any more and you have NOT changed the code at all, you need to report PMR to IBM.
Hope this helps,
Grofaty
Reply With Quote
  #3 (permalink)  
Old 09-12-06, 09:37
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
Quote:
Originally Posted by dollar489
It worked fine on FP5. Any idea?
Have your data changed since then, by any chance?
Reply With Quote
  #4 (permalink)  
Old 09-12-06, 13:51
dollar489 dollar489 is offline
Registered User
 
Join Date: Sep 2002
Posts: 456
Data has not changed. As a matter of fact our development environment is still at FP5 and it works fine there. Seems like optimizer is not going by the order of query but trying division by zero first.
Reply With Quote
  #5 (permalink)  
Old 09-12-06, 16:02
n_i n_i is offline
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,449
I'm wondering if IPI_LOW_BRNG_TMP may be null in your case. It wouldn't be equal to 0 then but might still generate a division by zero exception...
Reply With Quote
  #6 (permalink)  
Old 09-13-06, 04:04
grofaty grofaty is offline
Registered User
 
Join Date: Jan 2003
Posts: 1,570
n_i,
I think division by null should not return division by zero error. Formula: X / null returns: null.
Grofaty
Reply With Quote
  #7 (permalink)  
Old 06-07-09, 08:07
99sono 99sono is offline
Registered User
 
Join Date: Jun 2009
Posts: 14
Yup I am totally with you

"e. Seems like optimizer is not going by the order of query but trying division by zero first.
Reply With Quote"

I am using DB2 UDB, and i can totally tell you that the optimizer of DB2 is thinking way too much for my liking.

Here is a function that results in a similar DB2 error:

-------------------CODE EXAMPLE -------------------
DROP FUNCTION calculate_vcount@
CREATE FUNCTION calculate_vcount (inbetweendate date, nmonths int, nviews int)
RETURNS int
fbody: BEGIN ATOMIC
DECLARE stdvcount, lastvcount int;

IF nmonths <= 0 or inbetweendate > current date
THEN
RETURN 0;
END IF;

SET stdvcount = nviews/nmonths;
IF YEAR(inbetweendate) = YEAR(current date) and MONTH(inbetweendate) = MONTH(current date)
THEN
SET lastvcount = stdvcount + MOD(nviews,nmonths);
RETURN lastvcount;
END IF;
RETURN stdvcount;
END@
--------------------------

This function starts by checking if a date is bigger than it should be when it is used or if i have got a 0 input variable.
So i make sure i never divide by zero.
No matter, if the input to the function is Zero the damn DB2 evaluator summons up the conclusion that the division by zero will happen.
I have tried sorround the division statement inside of an IF clause but the result keeps on chaning.

So the only way this will not be a problem is if you make the if statement before perfoming the function invoking.

--1) USE AN INVALID DATE (expected result 0)
SELECT calculate_vcount(current date + 1 day, 0, 100)
FROM PROFILE@

This test fails no matter how well you try to program the function. You can not have a function have a division where the denomitor is zero, even if it is never called. IT is ridiculous but it seems to work that way.

Something similar happens with DB2 recursive functions. The optimizer tries to act smart and warns me that a recursive function that WILL never enter in an infinite loop may do so. Sometimes the people that code the DB2 should try to be less smart and more efficient.

... But well, whatever.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On