Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting Numeric overflow error for smaller numbers but success for larger numbers

Tags:

oracle

plsql

Following is my test function:

create or replace FUNCTION test
RETURN NUMBER
AS
  v_count   number(15);
  v_msisdn  number(15);
BEGIN
  v_msisdn:= 225952 * 10000;
  -- v_msisdn:=50510060853 * 10000;

  return v_msisdn;
END;

When I execute the function as

select test() from dual;

I get an error

1426. 00000 -  "numeric overflow"
*Cause:    Evaluation of an value expression causes an overflow/underflow.

However, if I update

v_msisdn:=50510060853 * 10000;

I do not get error.

Could some one please explain this behaviour as the second query is a bigger number?

like image 330
vineet dhar Avatar asked Sep 05 '18 14:09

vineet dhar


People also ask

How do you fix numeric overflow occurred during computation?

Answers. This error occurs when the given numerical value exceeds the limit of the data type. We can resolve this error by providing values within the limit of the data type.

What causes numeric computation overflow?

In computer programming, an integer overflow occurs when an arithmetic operation attempts to create a numeric value that is outside of the range that can be represented with a given number of digits – either higher than the maximum or lower than the minimum representable value.

What can cause an overflow error?

In general, a data type overflow error is when the data type used to store data was not large enough to hold the data. Furthermore, some data types can only store numbers up to a certain size. An overflow error will be produced, for example, if a data type is a single byte and the data to be stored is greater than 256.

What is signed integer overflow error?

An integer overflow error occurs when an operation makes an integer value greater than its maximum. For example, if the maximum value you can have is 100000, and your current value is 99999, then adding 2 will make it 'overflow'.


1 Answers

You can fix this by casting one of the integer literals to integer or number:

v_msisdn:= cast(225952 as integer) * 10000;

Small integer literals are treated like pls_integer for performance reasons. Unfortunately, this type raises an exception on overflow:

A calculation with two PLS_INTEGER values that overflows the PLS_INTEGER range raises an overflow exception, even if you assign the result to a NUMBER data type For calculations outside the PLS_INTEGER range, use INTEGER, a predefined subtype of the NUMBER data type.

In your second example, one of the values (50510060853) doesn't fit into the range of pls_integer and, thus, is interpreted as an integer.

Here's the similar question on AskTom: Datatype Number results in Numeric overflow although value is small enough

like image 50
default locale Avatar answered Sep 28 '22 03:09

default locale