3

Please see these two scenarios :

Case 1:

DECLARE
p1 PLS_INTEGER := 2147483647;
p2 INTEGER := 1;
n NUMBER;
BEGIN
n := p1 + p2;
END;

PL/SQL procedure successfully completed.

Case 2

DECLARE
p1 PLS_INTEGER := 2147483647;
p2 PLS_INTEGER := 1;
n NUMBER;
BEGIN
n := p1 + p2;
END;

Error at line 1
ORA-01426: numeric overflow
ORA-06512: at line 6

Why does case 2 failed, even I was trying to add same data type whereas case 1 executes successfully ?

3 Answers 3

1

Documentation for PLS_INTEGER and BINARY_INTEGER Data Types only says:

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.

But it does not tell you why.I assume the reason is PLS_INTEGER operations use hardware arithmetic, so Oracle properly does it internally like

n := CAST(p1 + p2 AS NUMBER);
Sign up to request clarification or add additional context in comments.

Comments

0

PLS_INTEGER is a PL/SQL data type which, according to the PL/SQL Language Reference:

stores signed integers in the range -2,147,483,648 through 2,147,483,647

On the other hand, INTEGER is an ANSI SQL Data Type which Oracle converts into a NUMBER with zero scale.

According to the Database SQL Language Reference, NUMBER is an Oracle Built-in Data Type which

stores zero as well as positive and negative fixed numbers with absolute values from 1.0 x 10^-130 to but not including 1.0 x 10^126

1 Comment

For what it's worth, the documentation also says the following: >> [PLS_INTEGER values require less storage.] >> [PLS_INTEGER operations use hardware arithmetic, so they are faster than NUMBER operations, which use library arithmetic.] So for any passers-by wondering which they should use, use PLS_INTEGER unless your application specifically requires a range greater than 2 billion
0

PLS_INTEGER can store upto 2,147,483,647. Crossing this, would result in overflow exception.

INTEGER is a predefined subtype of the NUMBER data type, and is usually referred as NUMBER(38). That means, it can go upto (10^39) - 1. So, no exception your first code block.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.