Search This Blog

Sunday, September 5, 2010

TYPE : User defined datatype


When ever a we create a type, it can be assumed as new data type created & in order to use it, we need to declare a varible of that type.

Think that oracle supplies NUMBER data type & we can't use the data type as a variable but we make use of a variable of the type NUMBER. 

In the same lines, 

Create a type ...............(register the user defined data type)
Declare a variable of the above create type and use that variable in the pl/sql block.


Example:
---------

Declare
x NUMBER;
y NUMBER;
begin
    x:=100;
    y:=200;
    dbms_output.put_line(x);
    dbms_output.put_line(y);
end;

Number is a datatype provided by oracle and we declare a variable of that datatype and use it in the code.



Declare
TYPE my_own_datatype is record (x number,y number);
my_record my_own_datatype;
begin
   my_record. x:=100;
    my_record.y:=200;
    dbms_output.put_line(my_record.x);
    dbms_output.put_line(my_record.y);
end;

my_own_datatype can be treated as user defined datatype which indirectly uses oracle defined datatypes.
my_record is a variable of type my_own_datatype.

if we need to use another variable, which is of same data type of an existing variable, we need to use %TYPE.

Example:
---------

Declare
x NUMBER;
y x%type;
begin
    x:=100;
    y:=200;
    dbms_output.put_line(x);
    dbms_output.put_line(y);
end;

Declare
TYPE my_own_datatype is record (x number,y number);
my_record my_own_datatype;
my_record_new my_record%type;

begin
   my_record. x:=100;
    my_record.y:=200;
    dbms_output.put_line(my_record.x);
    dbms_output.put_line(my_record.y);
end;

Note: my_record is of data type my_own_datatype and it can be assumed as user defined datatype, so no need %type.

But my_record_new is  referenced my_record datatype so we need to use %type;


No comments:

Post a Comment