Use Of Types in Oracle
Plsql types are used to store different type of datas as a single unit. It means a plsql type contains elements that are of different type eg: Number,Varchar2,table type,row type etc..
If we want to insert some datas into a table we can use a simple insert querry.Think about a situation like,we are inserting datas according to different conditions of data that exist in another table.In this situations we can use TYPES to store data from any point of the code into types and at the end just insert into that type to target table. The creation part comes under DECLARE section. We have to create an object for the type at the same time we create a type.
If we need only one column then we can go by the below way
Syntax: Type typename is <variable list with their types> ;
eg:
DECLARE
Type akhil_type IS table of
age varchar2(50);
var_akhil_type akhil_type;
Now var_akhil_type can be used as an object for the type. Throgh this object we can access any of the fields specified in it.
eg: var_akhil_type.age;
var_akhil_type.name='ALBERT';
Later the whole details will store in var_akhil_type .
If we want to insert theese datas that in tpes to a table, we can just mention like,
insert into employee values(var_akhil_type);
If we need to insert selected fields to a table then we can do it by
insert into employee (name) values (var_akhil_type.name);
If we have more than one columns to be in a type then we can use RECORD TYPE
DECLARE
TYPE rt_akhil_tp is RECORD (
age employee.age%type,
name employee.name%type,
manager employee.manager%type
);
Create a type for this particular record type
CREATE type t_akhil is rt_akhil_tp index by binary integer;
t_akhil lv_t_akhil;
begin
select age,name,manager into t_akhil from employee;
end;
If we want to insert some datas into a table we can use a simple insert querry.Think about a situation like,we are inserting datas according to different conditions of data that exist in another table.In this situations we can use TYPES to store data from any point of the code into types and at the end just insert into that type to target table. The creation part comes under DECLARE section. We have to create an object for the type at the same time we create a type.
If we need only one column then we can go by the below way
Syntax: Type typename is <variable list with their types> ;
eg:
DECLARE
Type akhil_type IS table of
age varchar2(50);
var_akhil_type akhil_type;
Now var_akhil_type can be used as an object for the type. Throgh this object we can access any of the fields specified in it.
eg: var_akhil_type.age;
var_akhil_type.name='ALBERT';
Later the whole details will store in var_akhil_type .
If we want to insert theese datas that in tpes to a table, we can just mention like,
insert into employee values(var_akhil_type);
If we need to insert selected fields to a table then we can do it by
insert into employee (name) values (var_akhil_type.name);
If we have more than one columns to be in a type then we can use RECORD TYPE
DECLARE
TYPE rt_akhil_tp is RECORD (
age employee.age%type,
name employee.name%type,
manager employee.manager%type
);
Create a type for this particular record type
CREATE type t_akhil is rt_akhil_tp index by binary integer;
t_akhil lv_t_akhil;
begin
select age,name,manager into t_akhil from employee;
end;
Comments
Post a Comment