Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between pls_integer and binary_integer?

I've inherited some code which is going to be the base for some additional work. Looking at the stored procs, I see quite a lot of associative-arrays.

Some of these are indexed by binary_integers, some by pls_integers. Are there any differences between the two?

I had a look at the documentation, but apart from this line:

The PL/SQL data types PLS_INTEGER and BINARY_INTEGER are identical. For simplicity, this document uses PLS_INTEGER to mean both PLS_INTEGER and BINARY_INTEGER.

I couldn't find any difference between the two. So what's the difference? Are both around for historical/compatibility reasons?

I'm using Oracle 10gR2

like image 516
Sathyajith Bhat Avatar asked Sep 14 '11 07:09

Sathyajith Bhat


People also ask

What is a PLS_INTEGER?

PLS_INTEGER is a PL/SQL data type used for storing signed integers. PLS_INTEGER is defined in the STANDARD package as a subtype (or rather a synonym) of BINARY_INTEGER. Variables declared as PLS_INTEGER can be assigned values between -2**31 to 2**31-1 (-2,147,483,648 to 2,147,483,647).

What is PLS_INTEGER in PL SQL?

The PLS_INTEGER data Type was introduced in PL/SQL version 2.2 and has a range of -2147483647 to 2147483647. Use of the PLS_INTEGER data type in PL/SQL involves less internal instructions to process, thus increasing performance. The PLS_INTEGER is useful for counters and integer operations.

What is index by BINARY_INTEGER in Oracle?

The INDEX BY clause must specify datatype BINARY_INTEGER, which has a magnitude range of -2147483647 .. 2147483647. If the element type is a record type, every field in the record must have a scalar datatype such as CHAR, DATE, or NUMBER.

Can PLS_INTEGER be null?

It is a sub-type of pls_integer and covers the same range. The basic difference is that simple_integer is always not null . When the value of the declared variable is never going to be null then you can declare it as simple_integer .


2 Answers

Historical reasons. They used to be different before 10g:

On 8i and 9i, PLS_INTEGER was noticeably faster than BINARY_INTEGER.


When it comes to declaring and manipulating integers, Oracle offers lots of options, including:

INTEGER - defined in the STANDARD package as a subtype of NUMBER, this datatype is implemented in a completely platform-independent fashion, which means that anything you do with NUMBER or INTEGER variables should work the same regardless of the hardware on which the database is installed.

BINARY_INTEGER - defined in the STANDARD package as a subtype of INTEGER. Variables declared as BINARY_INTEGER can be assigned values between -231+1 .. 231-1, aka -2,147,483,647 to 2,147,483,647. Prior to Oracle9i Database Release 2, BINARY_INTEGER was the only indexing datatype allowed for associative arrays (aka, index-by tables), as in:

  TYPE my_array_t IS TABLE OF VARCHAR2(100)    INDEX BY BINARY_INTEGER 

PLS_INTEGER - defined in the STANDARD package as a subtype of BINARY_INTEGER. Variables declared as PLS_INTEGER can be assigned values between -231+1 .. 231-1, aka -2,147,483,647 to 2,147,483,647. PLS_INTEGER operations use machine arithmetic, so they are generally faster than NUMBER and INTEGER operations. Also, prior to Oracle Database 10g, they are faster than BINARY_INTEGER. In Oracle Database 10g, however, BINARY_INTEGER and PLS_INTEGER are now identical and can be used interchangeably.

like image 181
Thilo Avatar answered Sep 19 '22 16:09

Thilo


binary_integer and pls_integer both are same. Both are PL/SQL datatypes with range -2,147,648,467 to 2,147,648,467.

Compared to integer and binary_integer pls_integer very fast in excution. Because pls_intger operates on machine arithmetic and binary_integer operes on library arithmetic.

pls_integer comes from oracle10g.

binary_integer allows indexing integer for assocative arrays prior to oracle9i.

Clear example:

SET TIMING ON  declare   num   integer := 0;   incr  integer := 1;   limit integer := 100000000; begin   while num < limit loop     num := num + incr;   end loop; end; PL/SQL procedure successfully completed.  Elapsed: 00:00:20.23 ex:2 declare   num   binary_integer := 0;   incr  binary_integer := 1;   limit binary_integer := 100000000; begin   while num < limit loop     num := num + incr;   end loop; end; /   PL/SQL procedure successfully completed.  Elapsed: 00:00:05.81 ex:3 declare   num   pls_integer := 0;   incr  pls_integer := 1;   limit pls_integer := 100000000; begin   while num < limit loop     num := num + incr;   end loop; end; /  
like image 22
nagu Avatar answered Sep 22 '22 16:09

nagu