Saturday, October 01, 2005

A word abt INTEGERS

Recently, I gotta chance to do something which changed my way of understanding .Believed me until that point thought an integer is a number that can only store whole numbers (or more mathematically expressed: the positive natural number 1, 2, 3..., the negative natural numbers -1, -2, -3, as well as zero). Unfortunately, that is not always the case with Oracle.

                declare
i integer;
n number(38);
begin
i := 3.14159265;
n := 2.17111891;
dbms_output.put_line('i: ' || i);
dbms_output.put_line('n: ' || n);
end;
/

try this u will find the expected answer, the variables cannot store
the fractional parts of the numbers being assigned to the variables.
In fact, Oracle rounds the numbers towards the closest natural number:

i: 3
n: 3

Now let us remove the declaration part & try the following
create procedure takes_integer(i in integer, p in pls_integer)
is
begin
dbms_output.put_line('i [integer]: ' || i || ', p [pls_integer]: ' || p);
end takes_integer;
/


Things change slightly, but significantly, when the declaration
is not in the DECLARE part of a PL/SQL block but in the argument
list of a procedure.
Exec this
exec takes_integer(3.14159265, 2.71828183);

This procedure is called, again with two non-natural numbers
did u get surprised by the result
i [integer]: 3.14159265, p [pls_integer]: 3

The argument i an integer can obviously store non integers.
However, a pls_integer can not.

Point to be kept in mind :
if you want to have an integer passed to a procedure/function,
use pls_integer instead of integer. so that u canmake sure that
u are passing only the integer not as non-integer.

how it happened in case of procedure ??
try desc ing the procedure
desc takes_integer;

SQL> desc takes_integer
PROCEDURE takes_integer
Argument Name Type In/Out Default?
------------------------------ ----------------------- ------ --------
I NUMBER(38) IN
P BINARY_INTEGER IN


An integer, being an ansi_datatype , is (rightly) implicitely converted
into number(38).But number(38) should not be able to stored 3.14159265.

0 comments: