%TYPE
Say we have a table as under
Create Table tblEmployee
(
EmpName VARCHAR2(50)
,Salary INT
);
It is a simple table with two columns viz. EmpName of type VARCHAR2 and Salary of Type INT
Consider we have variables for holding the datatype of a table's column.
DECLARE
var_EmpName VARCHAR(50) := 'TestEmp';
var_Salary INT ;
BEGIN
/* Some PL/SQLcode */
Select Salary
Into var_Salary
From tblEmployee
Where EmpName = var_EmpName;
END;
We can make out that we are storing the Salary record in the var_Salary field for a particular employee. Now imagine that we have many store procedures where we need to define the 'var_Salary' as INT. Now because of some business need, we need to change the initial datatype of the field 'Salary' from INT to NUMBER as under
Alter Table tblEmployee
Modify ( Salary Number(7,2));
Now imagine the pain of changing the datatype of 'var_Salary' from INT to NUMBER in all those stored procedures where it has been used. For eleiminating this problem, we have %TYPE. It specifies the type to the variable same as specified for the column and accomodate changes made in the colunn data types.
DECLARE
var_EmpName tblEmployee.EMPNAME%TYPE := 'TestEmp';
var_Salary tblEmployee.SALARY%TYPE;
BEGIN
/* Some PL/SQLcode */
Select Salary
Into var_Salary
From tblEmployeetest
Where EmpName = var_EmpName;
DBMS_OUTPUT.PUT_LINE(var_Salary);
END;
/
As can be figure out that, now the variables are strictly typed.
The general syntax can be: Variable_Name Table_Name.Column_Name%Type
Hope this is useful.Thanks for reading