Sub topic 12
Matching a Whole Row with `%ROWTYPE`
Assume you know basic SELECT statements, Oracle data types, and how to run an anonymous PL/SQL block in SQL*Plus, SQL Developer, or SQLcl. This chapter uses the sample employees table from the HR schema.
What `%ROWTYPE` Represents
A PL/SQL record is a variable that contains several related fields. The %ROWTYPE attribute creates a record whose fields match the columns of a table or view.
For example, employees%ROWTYPE creates a record with fields such as:
employee_idfirst_namelast_nameemailsalary- Other columns in
employees
The record does not contain every row in the table. It contains one row's values at a time.
Declaring and Filling a Record
Declare a row record in the declaration section. Then use SELECT * INTO to load one complete row.
SET SERVEROUTPUT ON
DECLARE
v_emp employees%ROWTYPE;
BEGIN
SELECT *
INTO v_emp
FROM employees
WHERE employee_id = 100;
DBMS_OUTPUT.PUT_LINE(
v_emp.first_name || ' ' || v_emp.last_name
);
DBMS_OUTPUT.PUT_LINE('Salary: ' || v_emp.salary);
END;
/The record name is v_emp. Access a field with dot notation, such as v_emp.last_name.
The query 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.
Handling Query Results Safely
An exception section can handle these two outcomes.
SET SERVEROUTPUT ON
DECLARE
v_emp employees%ROWTYPE;
BEGIN
SELECT *
INTO v_emp
FROM employees
WHERE employee_id = -1;
DBMS_OUTPUT.PUT_LINE(
v_emp.first_name || ' ' || v_emp.last_name
);
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee was not found.');
WHEN TOO_MANY_ROWS THEN
DBMS_OUTPUT.PUT_LINE('The query returned several employees.');
END;
/The WHERE condition should normally use a unique key, such as employee_id, when you expect one row. A nonunique condition, such as department_id = 50, might return several rows and cannot be loaded into one record.
`%ROWTYPE` and `%TYPE`
Use %TYPE when you need one variable whose type follows one column:
v_salary employees.salary%TYPE;Use %ROWTYPE when you need one record containing all columns from a row:
v_emp employees%ROWTYPE;Both attributes anchor PL/SQL declarations to the database definition. If the column's data type changes, a dependent PL/SQL unit can be recompiled with the updated type.
%ROWTYPE does not give the record extra columns that are calculated by a query. The selected expressions must fit the record fields. For a partial or calculated result, declare individual variables or a custom record instead.
Important Limitations
A %ROWTYPE record is a PL/SQL value. You can read and change its fields inside PL/SQL, but you cannot print the entire record with one DBMS_OUTPUT.PUT_LINE call. Choose individual fields.
You also cannot use a record as a single SQL value in a normal WHERE condition. Refer to its fields separately when SQL needs them.
The record follows the table or view definition. This is convenient, but it also means that code using SELECT * depends on that definition. Select specific columns when you need a stable interface or when performance and clarity matter.
Exercise
What happens when SELECT * INTO v_emp FROM employees WHERE department_id = 50 matches several rows, and how could you process all matching employees instead?
Show answer
Oracle raises TOO_MANY_ROWS because one record can hold only one row. Use an explicit cursor or a cursor FOR loop to process each matching row.