Documente Academic
Documente Profesional
Documente Cultură
bnr.oracleapps@gmail.com
By Nagaraju Byri
PL/SQL Records
What are records?
Records are another type of datatypes which oracle allows to be defined as a placeholder. Records are composite
datatypes, which means it is a combination of different scalar datatypes like char, varchar, number etc. Each
scalar data types in the record holds a value. A record can be visualized as a row of data. It can contain all the
contents of a row.
Different Types of Records
PL/SQL supports three different kinds of records: table-based, cursor-based, and programmer-defined. These
different types of records are used in different ways and for different purposes, but all three share the same
internal structure: every record is composed of one or more fields. However, the way these fields are defined in
the record depend on the record type. Table 9.1 shows this information about each record type.
PL/SQL Record Types
Record Type
Description
Fields in Record
Table-based
A record based on a table's column Each field corresponds to -- and has the same name as -structure.
a column in a table.
Cursor-based
A record based on the cursor's Each field corresponds to a column or expression in the
SELECT statement.
cursor SELECT statement.
Programmerdefined
A record whose structure you, the Each field is defined explicitly (its name and datatype)
programmer, get to define with a in the TYPE statement for that record; a field in a
declaration statement.
programmer-defined record can even be another record.
Declaring a record:
To declare a record, you must first define a composite datatype; then declare a record for that type.
The General Syntax to define a composite datatype is:
There are different ways you can declare the datatype of the fields.
1) You can declare the field in the same way as you declare the fieds while creating the table.
2) If a field is based on a column from database table, you can define the field_type as follows:
col_name table_name.column_name%type;
By declaring the field datatype in the above method, the datatype of the column is dynamically applied to
the field. This method is useful when you are altering the column specification of the table, because you do
not need to change the code again.
NOTE: You can use also %type to declare variables and constants.
The General Syntax to declare a record of a uer-defined datatype is:
record_name record_type_name;
The following code shows how to declare a record called employee_rec based on a user-defined type.
DECLARE
TYPE employee_type IS RECORD
(employee_id number(5),
employee_first_name varchar2(25),
employee_last_name employee.last_name%type,
employee_dept employee.dept%type);
employee_salary employee.salary%type;
employee_rec employee_type;
If all the fields of a record are based on the columns of a table, we can declare the record as follows:
record_name table_name%ROWTYPE;
Page 1 of 5
Oracle Records
bnr.oracleapps@gmail.com
By Nagaraju Byri
DECLARE
employee_rec employee%ROWTYPE;
record_name.col_name := value;
If you used %ROWTYPE to declare a record, you can assign values as shown:
record_name.column_name := value;
We can assign values to records using SELECT Statements as shown:
If %ROWTYPE is used to declare a record then you can directly assign values to the whole record instead of
each columns separately. In this case, you must SELECT all the columns from the table into the record as
shown:
var_name := record_name.col_name;
The following table consolidates the different ways you can assign values to and from a record:
Syntax
Usage
record_name.col_name := value;
To directly assign a value to a specific column
of a record.
record_name.column_name := value;
To directly assign a value to a specific column
of a record, if the record is declared using
%ROWTYPE.
SELECT col1, col2 INTO record_name.col_name1,
To assign values to each field of a record
record_name.col_name2 FROM table_name [WHERE
from the database table.
clause];
SELECT * INTO record_name FROM table_name [WHERE
To assign a value to all fields in the record
clause];
from a database table.
variable_name := record_name.col_name;
To get a value from a record column and
Page 2 of 5
Oracle Records
bnr.oracleapps@gmail.com
By Nagaraju Byri
assigning it to a variable.
Term: RECORD
Definition:
In database terminology, a RECORD is row in a table where each column contains a single value.
In Oracle PL/SQL, it is a set of logical attributes. Each attribute carries its own name and data type. In terms of behaviour, it is
analogous to an object type in SQL whose scope and visibility is within a PL/SQL block. Note that it cannot be created or stored
in database.
Example Syntax:
A PL/SQL record can be declared in two ways.
1. Declare it as a block variable using %ROWTYPE attribute using the following syntax:
[VARIABLE] TABLE%ROWTYPE
Using above declaration, the record will hold the same structure as the table record.
2. Declare record as a type. It defines the structure of its own. A variable must be declared of this type to use it in the block.
DECLARE
TYPE [NAME] IS RECORD
( attribute declaration [, attribute declaration] )
Example Usage:
The below PL/SQL block declares two PL/SQL records T_REC and L_TABREC. Note that T_REC is a type and owns its structure.
L_TABREC uses %ROWTYPE notation to inherit structure of an EMPLOYEE record.
DECLARE
TYPE T_REC IS RECORD
(EMPID NUMBER, SAL NUMBER, ENAME VARCHAR2(100));
L_TREC T_REC;
L_TABREC EMPLOYEE%ROWTYPE;
BEGIN
SELECT EMPNO, SAL, ENAME
INTO L_TREC
FROM EMPLOYEE
WHERE EMPNO = 100;
SELECT *
INTO L_TABREC
FROM EMPLOYEE
WHERE EMPNO = 100;
END;
Page 3 of 5
Oracle Records
bnr.oracleapps@gmail.com
By Nagaraju Byri
'||V_SAL);
/* Program to display all employees empno, ename and salary with using TABLE BASED RECORD
type*/
DECLARE
CURSOR EMP_CUR IS
SELECT * FROM EMP;
v_emp_rec EMP%ROWTYPE;
BEGIN
OPEN EMP_CUR;
LOOP
FETCH EMP_CUR INTO v_emp_rec;
EXIT WHEN EMP_CUR%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_emp_rec.empno||' '||v_emp_rec.ENAME||'
END LOOP;
END;
'||v_emp_rec.SAL);
Disadvantage:
Actually v_emp_rec rowtype of variable is declared as emp table row in plsql means all the columns data
is available with in the plsql memory.
8 columns data is there in memory but the user is required only 3 columns data wastage of Memory.
select * from emp;
Page 4 of 5
Oracle Records
bnr.oracleapps@gmail.com
By Nagaraju Byri
/* Program to display all employees empno, ename and salary with using Cursor-based RECORD type*/
DECLARE
CURSOR EMP_CUR IS
SELECT empno,ename,sal FROM EMP;
v_emp_rec EMP_CUR%ROWTYPE;
BEGIN
OPEN EMP_CUR;
LOOP
FETCH EMP_CUR INTO v_emp_rec;
EXIT WHEN EMP_CUR%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_emp_rec.empno||'
'||v_emp_rec.ENAME||'
'||v_emp_rec.SAL);
END LOOP;
END;
/* Program to display all employees empno, ename and salary with using Programmer-defined
RECORD type*/
DECLARE
CURSOR EMP_CUR IS
SELECT empno,ename,sal,sal*12 FROM EMP;
TYPE emp_rec_type IS RECORD
( empno emp.empno%type,
ename emp.ename%type,
sal emp.sal%type,
asal number);
v_emp_rec emp_rec_type;
BEGIN
OPEN EMP_CUR;
LOOP
FETCH EMP_CUR INTO v_emp_rec;
EXIT WHEN EMP_CUR%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_emp_rec.empno||' '||v_emp_rec.ENAME||'
END LOOP;
END;
Page 5 of 5
'||v_emp_rec.SAL||' '||v_emp_rec.asal);