In my previous article i have explained about the different datatypes of oracle with examples.In this article i will try to explain the PL SQL complex datatypes with examples.The complex datatypes are those which holds the data in to different record types or arrays.PL SQL Record Type are nothing but the composite datatype which holds the values of different scalar datatypes together.
PL SQL Record Type are types which holds the combination of different scalar datatypes together.
Record Type is the complex data type which allows the programmer to create a new data type with the desirable column structure.
There are so many situation where user needs to use the different types of columns together.So PL SQL gives us the solution to define our own datatype which contains different scalar datatypes which is also known as record types.Following are some bullet points user needs to remember :
Creation of record type is at program level.
Syntax :
Create type type_name is Record
(Column1 Datatype(size),
Column2 Datatype(size), …
…Column’N’ Datatype(size));
The Create type statement is used to create record type of specific datatypes.This is given as individual statement not inside the block.
If user wants to use the specific datatype of Student table which contains Student information like Roll_No,Name and subjects of student.
Create type Student_Type is Record
(Roll_No number,
Name Varchar2(30),
Subjects Varchar2(30));
Output :
Type Created
Here the datatype named Student_Type is created at database level. User can use that record type in any program.This record type is reusable to different PL SQL blocks.
The following syntax is used to define the record type.The Declaration of type is always at subprogram level :
TYPE Name_of_Record IS RECORD
(Column1 Datatype(size),
Column2 Datatype(size), …
…Column’N’ Datatype(size));
Name_of_Record :
The Name_of_Record is nothing but the name given to that specific record.This should be the name of name of composite type you want to define.
Column1..Column’N’ :
These are nothing but different names of fields user wants to use to create record types.
Datatypes :
The Datatypes are nothing but different scalar datatypes.
Example of Record Type at program level :
Declare —Declaration of type variable
type Student_Type is Record
(Roll_No number,
Name Varchar2(30),
Subjects Varchar2(30));
Type_Stud Student_Type; —Here We need to create the variable for new type declared
Begin
Type_Stud.Roll_No:=’1′;
Type_Stud.Name:=’Amit’;
Type_Stud.Subjects:=’Maths’;
dbms_output.put_line(‘Student Information:’||Type_Stud.Roll_No||’ ‘||Type_Stud.Name||’ ‘||Type_Stud.Subjects);
end;
Output :
Student Information : 1 Amit Maths
These are some examples of Record type in PL SQL.This means user can create record type in program as well as separately also.
Example of Record Type at subprogram level for row level access :
Declare —Declaration of type variable
type Student_Type is Record
(Roll_No number,
Name Varchar2(30),
Subjects Varchar2(30));
Type_Stud Student_Type; —Here We need to create the variable for new type declared
Begin
Insert into Student
values (‘1′,’Amit’,’Maths’);
Commit;
Select Roll_No, Name ,Subjects into Student_Type from student where Roll_No=’1′;
dbms_output.put_line(‘Student Information:’||Type_Stud.Roll_No||’ ‘||Type_Stud.Name||’ ‘||Type_Stud.Subjects);
end;
Output :
Student Information : 1 Amit Maths
The record type can be accessed only in column level while redirecting its value to any output mode. The record types are used by pl sql to define combination of different scalar types. Hope this article is useful for everyone.Please comment in comment section if you like this article.
In my previous article I have given roles for L1 and L2 support engineer with…
I have started this new series of how to become application support engineer. This article…
In this series we are starting with Roles and responsibilities of L1 support engineer .…
Introduction Cryptocurrencies took the world by storm, setting up a new financial system and breaking…
In my previous article I have given Top 20 technical support interview questions with its…
In my previous articles I have given 15 most asked desktop support interview questions with…