This topic describes how to use %TYPE in variable declarations.
Overview
A variable is declared in an SPL program to store values from tables in a database. To ensure compatibility between table columns and SPL variables, the data types of the columns and the variables must be the same. If you change the data type of a column, you must change the data type of the variables in the corresponding SPL program. You can use the column attribute %TYPE. This way, you do not need to encode a specific column data type as variable declaration. A qualified column name in dot notation or the name of a previously declared variable must be specified as the prefix of %TYPE. The data type of the column or the variable prefixed to %TYPE is assigned to the variable to be declared. If the data type of the given column or variable changes, the new data type is associated with the variable. This eliminates the need to modify the declaration.
Syntax
name { { table | view }.column | variable }%TYPE;
Parameter
Field | Overview |
---|---|
name | The formal parameter to be declared, or the identifier that is assigned to the variable. |
column | The name of a column in a table or in a view. |
variable | The name of the variable that was declared prior to the variable identified by name. |
:=
assignment operator.
Example
In the following stored procedure, the system queries the emp
table by using an employee number, obtains the average salary of all the employees
in the department to which the employee belongs, and compares the salary of the employee
with the average salary of the department.
CREATE OR REPLACE PROCEDURE emp_sal_query (
p_empno IN NUMBER
)
IS
v_ename VARCHAR2(10);
v_job VARCHAR2(9);
v_hiredate DATE;
v_sal NUMBER(7,2);
v_deptno NUMBER(2);
v_avgsal NUMBER(7,2);
BEGIN
SELECT ename, job, hiredate, sal, deptno
INTO v_ename, v_job, v_hiredate, v_sal, v_deptno
FROM emp WHERE empno = p_empno;
DBMS_OUTPUT.PUT_LINE('Employee # : ' || p_empno);
DBMS_OUTPUT.PUT_LINE('Name : ' || v_ename);
DBMS_OUTPUT.PUT_LINE('Job : ' || v_job);
DBMS_OUTPUT.PUT_LINE('Hire Date : ' || v_hiredate);
DBMS_OUTPUT.PUT_LINE('Salary : ' || v_sal);
DBMS_OUTPUT.PUT_LINE('Dept # : ' || v_deptno);
SELECT AVG(sal) INTO v_avgsal
FROM emp WHERE deptno = v_deptno;
IF v_sal > v_avgsal THEN
DBMS_OUTPUT.PUT_LINE('Employee''s salary is more than the '
|| 'department average of ' || v_avgsal);
ELSE
DBMS_OUTPUT.PUT_LINE('Employee''s salary does not exceed the '
|| 'department average of ' || v_avgsal);
END IF;
END;
The preceding stored procedure can also be written in another mode, as shown in the
following code block. This way, you do not need to explicitly encode the data types
of the emp
table in the declaration section of the stored procedure.
CREATE OR REPLACE PROCEDURE emp_sal_query (
p_empno IN emp.empno%TYPE
)
IS
v_ename emp.ename%TYPE;
v_job emp.job%TYPE;
v_hiredate emp.hiredate%TYPE;
v_sal emp.sal%TYPE;
v_deptno emp.deptno%TYPE;
v_avgsal v_sal%TYPE;
BEGIN
SELECT ename, job, hiredate, sal, deptno
INTO v_ename, v_job, v_hiredate, v_sal, v_deptno
FROM emp WHERE empno = p_empno;
DBMS_OUTPUT.PUT_LINE('Employee # : ' || p_empno);
DBMS_OUTPUT.PUT_LINE('Name : ' || v_ename);
DBMS_OUTPUT.PUT_LINE('Job : ' || v_job);
DBMS_OUTPUT.PUT_LINE('Hire Date : ' || v_hiredate);
DBMS_OUTPUT.PUT_LINE('Salary : ' || v_sal);
DBMS_OUTPUT.PUT_LINE('Dept # : ' || v_deptno);
SELECT AVG(sal) INTO v_avgsal
FROM emp WHERE deptno = v_deptno;
IF v_sal > v_avgsal THEN
DBMS_OUTPUT.PUT_LINE('Employee''s salary is more than the '
|| 'department average of ' || v_avgsal);
ELSE
DBMS_OUTPUT.PUT_LINE('Employee''s salary does not exceed the '
|| 'department average of ' || v_avgsal);
END IF;
END;
- p_empno is an example of a formal parameter that is explicitly defined by using %TYPE.
- v_avgsal shows how to use %TYPE to reference another variable instead of a table column.
Sample result:
EXEC emp_sal_query(7698);
Employee # : 7698
Name : BLAKE
Job : MANAGER
Hire Date : 01-MAY-81 00:00:00
Salary : 2850.00
Dept # : 30
Employee's salary is more than the department average of 1566.67