Sub topic 11
Anchoring Data Types with %TYPE
Assume you know basic SQL, Oracle data types, and how to run an anonymous PL/SQL block in SQL*Plus, SQL Developer, or SQLcl. This chapter uses the HR.EMPLOYEES table.
Why anchor a data type?
A PL/SQL variable needs a data type. You can declare it directly:
v_salary NUMBER(8,2);This works, but it duplicates information already stored in the table definition. If the employees.salary column changes, the declaration might become inaccurate.
The %TYPE attribute lets a variable take its data type from a table column or another PL/SQL variable. The declaration remains connected to the source definition.
variable_name table_name.column_name%TYPE;The database determines the variable's type when it compiles the PL/SQL block. The variable does not automatically receive a value. %TYPE describes the type only.
Using a column with %TYPE
The following block declares two variables whose types follow columns in HR.EMPLOYEES. It then loads one employee into those variables with SELECT INTO.
SET SERVEROUTPUT ON
DECLARE
v_last_name employees.last_name%TYPE;
v_salary employees.salary%TYPE;
BEGIN
SELECT last_name, salary
INTO v_last_name, v_salary
FROM employees
WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE(
v_last_name || ' earns ' || v_salary
);
END;
/The number of expressions in the SELECT list must match the number of variables in the INTO list. Their positions must also match. Here, last_name goes into v_last_name, and salary goes into v_salary.
SELECT INTO must return exactly one row. If it returns no rows, Oracle raises NO_DATA_FOUND. If it returns multiple rows, Oracle raises TOO_MANY_ROWS. %TYPE does not change this rule.
Anchoring to another variable
You can also anchor one variable to another:
SET SERVEROUTPUT ON
DECLARE
v_salary employees.salary%TYPE;
v_new_salary v_salary%TYPE;
BEGIN
SELECT salary
INTO v_salary
FROM employees
WHERE employee_id = 100;
v_new_salary := v_salary * 1.10;
DBMS_OUTPUT.PUT_LINE('Old: ' || v_salary);
DBMS_OUTPUT.PUT_LINE('New: ' || v_new_salary);
END;
/The referenced variable must be declared earlier in the same declarative region. This pattern is useful when several variables should share one definition.
Common errors and limitations
- Write
%TYPEafter the column or variable name, as inemployees.salary%TYPE. - Qualify a table column with its table name.
salary%TYPEis not valid unlesssalaryis a declared PL/SQL variable. - Do not expect
%TYPEto copy a value. Assign a value separately or useSELECT INTO. - Do not assume
%TYPEmakes a variableNOT NULL. AddNOT NULLand an initial value when that behavior is required. - Remember that a schema change can make a dependent PL/SQL object invalid until Oracle recompiles it.
A scalar variable represents one value. Use %ROWTYPE when you need a record with one field for every column in a table row. Use %TYPE when you need only one column's type.
| Requirement | Suitable declaration |
|---|---|
| One value matching `employees.salary` | `employees.salary%TYPE` |
| A value matching another variable | `v_salary%TYPE` |
| All columns from one employee row | `employees%ROWTYPE` |
Exercise
What happens if the SELECT statement in the first example returns two employees instead of one?
Show answer
Oracle raises TOOMANYROWS because SELECT INTO requires exactly one row.