0% found this document useful (0 votes)
25 views5 pages

Wa0025.

The document explains the use of %TYPE and %ROWTYPE attributes in PL/SQL for declaring variables that match the datatype of table columns and entire rows, respectively. It highlights the advantages of these attributes, such as not needing to know the exact datatypes and automatic updates when database definitions change. Several examples demonstrate how to use these attributes to retrieve and display employee information from a database.

Uploaded by

saniyachandere12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

Wa0025.

The document explains the use of %TYPE and %ROWTYPE attributes in PL/SQL for declaring variables that match the datatype of table columns and entire rows, respectively. It highlights the advantages of these attributes, such as not needing to know the exact datatypes and automatic updates when database definitions change. Several examples demonstrate how to use these attributes to retrieve and display employee information from a database.

Uploaded by

saniyachandere12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

%type and %rowtype

%type
Syntax:-
Variable_name <table_name>. <column_name>%type;

Using the %TYPE Attribute to Declare Variables

The %TYPE attribute provides the datatype of a variable or table column. This
is particularly useful when declaring variables that will hold values of a table
column. For example, suppose you want to declare variables as the same
datatype as the employee_id and last_name columns in employees table. To
declare variables named empid and emplname that have the same datatype as
the table columns, use dot notation and the %TYPE attribute.

Eid emp.emp_id%type;
Ln emp.Lname%type;

Declaring variables with the %TYPE attribute has two advantages. First, you do
not need to know the exact datatype of the table columns. Second, if you change
the database definition of columns, such as employee_id or last_name, the
datatypes of empid and emplname in Example: Using %TYPE With Table
Columns in PL/SQL change accordingly at run time.
Q.1 Display employee ename whose employee number is 7900.
declare
en emp.ename%type;
begin
select ename into en from emp where empno=7900;
dbms_output.put_line('Employee name is '|| en);
end;
/
Employee name is JAMES

PL/SQL procedure successfully completed.


Q.2 Display employee salary and department number whose employee number
is 7902.

SQL> declare
dn emp.deptno%type;
s emp.sal%type;
begin
select sal,deptno into s,dn from emp where empno=7902;
dbms_output.put_line('Salary is '|| s || 'Deptno is '||dn);
end;
/
Salary is 3000Deptno is 20

PL/SQL procedure successfully completed.


Q.3 Display employee salary and department number accept employee number
from user.

SQL> declare
dn emp.deptno%type;
s emp.sal%type;
en emp.empno%type;
begin
en:=&en;
select sal,deptno into s,dn from emp where empno=en;
dbms_output.put_line('Salary is '|| s || 'Deptno is '||dn||' employee number is
'||en);
end;
/
Enter value for en: 7788
old 6: en:=&en;
new 6: en:=7788;
Salary is 3000Deptno is 20 employee number is 7788

PL/SQL procedure successfully completed.


%rowtype
Syntax: -
Variable_name <table_name>%rowtype;
Using the %ROWTYPE Attribute to Declare Variables
For easier maintenance of code that interacts with the database, you can use
the %ROWTYPE attribute to declare a variable that represents a row in a table.
A PL/SQL record is the datatype that stores the same information as a row in a
table.
In PL/SQL, records are used to group data. A record consists of a number of
related fields in which data values can be stored. The record can store an entire
row of data selected from the table or fetched from a cursor or cursor variable.
For information about records, see "Using Record Types".
Columns in a row and corresponding fields in a record have the same names and
datatypes. In Example: Using %ROWTYPE with a PL/SQL Record, you
declare a record named emp_rec. Its fields have the same names and datatypes
as the columns in the employees table. You use dot notation to reference fields,
such as emp_rec.last_name.
Declaring variables with the %ROWTYPE attribute has several advantages.
First, you do not need to know the exact datatype of the table columns. Second,
if you change the database definition of any of the table columns, the datatypes
associated with the %ROWTYPE declaration change accordingly at run time.

Q1.
SQL> declare
rec emp%rowtype;
begin
select * into rec from emp where empno=7788;
dbms_output.put_line('Employee name is '|| rec.ename);
end;
/
Employee name is SCOTT

PL/SQL procedure successfully completed.

SQL> declare
rec emp%rowtype;
begin
select * into rec from emp where empno=7788;
dbms_output.put_line('Employee name is '|| rec.ename ||' employee sal is
'||rec.sal);
end;
/
Employee name is SCOTT employee sal is 3000

PL/SQL procedure successfully completed.

You might also like