HomeOracle Page 3 - Database Interaction with PL/SQL, part 2
TYPE with RECORD declaration - Oracle
This article picks up where part one left off. You will learn more about how to use %ROWTYPE and %TYPE, and be introduced to TYPE, RECORD, and TABLE declarations in PL/SQL.
Until now, we have been working either with %TYPE or %ROWTYPE. This means we are working with either one value or one complete record. How do we create our own data type, with our own specified number of values to hold? This is where TYPE and RECORD come in. Those who are familiar with the C language can consider this to be a structure declaration. Let me explain this to you in detail.
Let us consider a table of about 20 columns. I always need to work with only seven of those columns. If I use %ROWTYPE, I get all 20 values unnecessarily. At the same time, my program will be bit clumsy if I use seven %TYPE declarations. A better way to solve this solution is by defining my own data type, which can hold seven values. Then I declare the variables of my new data type and work with them. The following example illustrates this.
Let us examine the above program part by part. First, consider the following:
TYPE t_emprec IS RECORD
(
name emp.ename%type,
salary emp.sal%type,
job emp.job%type
);
The above defines a new data type named "t_emprec" (just like %ROWTYPE with limited specified fields) which can hold three fields, namely "name," "salary" and "job."
v_emp t_emprec;
The above statement declares a variable "v_emp" based on the datatype "t_emprec." This means that "v_emp" internally contains the fields "name," "salary" and "job."
select ename,sal,job into v_emp
from emp where empno = v_empno;
The above statement places the values of "ename," "sal" and "job" into fields "name," "salary" and "job" of the variable "v_emp" respectively.