Sub topic 8
Getting Changed Values Back with `RETURNING INTO`
Assume you already know basic SELECT, INSERT, UPDATE, and DELETE statements, as well as primary keys and transactions. In Oracle, the RETURNING INTO clause sends values changed by a DML statement directly into PL/SQL variables. DML means data manipulation language, such as INSERT, UPDATE, and DELETE.
Why use `RETURNING INTO`?
Without RETURNING INTO, an application often changes a row and then runs another SELECT to read the result. That adds another statement and can repeat the lookup logic. RETURNING INTO gets the changed value from the same DML operation.
| DML statement | Useful returned value |
|---|---|
| `INSERT` | A generated identity or sequence value |
| `UPDATE` | A newly calculated salary or timestamp |
| `DELETE` | A value from the row before removal |
The returned values go into variables. Therefore, the clause is most commonly used inside a PL/SQL block or procedure.
sequenceDiagram
participant P as PL/SQL block
participant O as Oracle
participant V as Variable
P->>O: UPDATE ... RETURNING salary
O-->>V: Store changed salary
P->>V: Use or display the valueReturning a value from `UPDATE`
This block raises Ada's salary and stores the new value in v_new_salary:
DECLARE
v_new_salary emp_demo.salary%TYPE;
BEGIN
UPDATE emp_demo
SET salary = salary + 100
WHERE emp_id = 1
RETURNING salary INTO v_new_salary;
DBMS_OUTPUT.PUT_LINE('New salary: ' || v_new_salary);
COMMIT;
END;
/%TYPE gives the variable the same data type as the table column. This keeps the PL/SQL variable aligned with the schema.
The RETURNING expression is evaluated after the change. In this example, the variable receives the increased salary, not the old salary.
The row-count requirement
A scalar variable is suitable when the statement affects exactly one row. If the WHERE clause matches no rows, Oracle raises NO_DATA_FOUND. If it matches multiple rows, Oracle raises TOO_MANY_ROWS.
For multiple rows, use collection variables with BULK COLLECT:
DECLARE
TYPE salary_list IS TABLE OF emp_demo.salary%TYPE;
v_salaries salary_list;
BEGIN
UPDATE emp_demo
SET salary = salary * 1.05
WHERE dept_id = 20
RETURNING salary BULK COLLECT INTO v_salaries;
DBMS_OUTPUT.PUT_LINE('Rows changed: ' || v_salaries.COUNT);
ROLLBACK;
END;
/BULK COLLECT places one returned value for each affected row into a collection. The order of collection elements should not be treated as a business ordering unless you design for that separately.
Returning values from `INSERT` and `DELETE`
You can return values from other DML statements too. This example returns a generated identity value after inserting a row:
DECLARE
v_emp_id emp_demo.emp_id%TYPE;
BEGIN
INSERT INTO emp_demo (emp_id, emp_name, dept_id, salary)
VALUES (3, 'Lin', 20, 8500)
RETURNING emp_id INTO v_emp_id;
DBMS_OUTPUT.PUT_LINE('Inserted employee: ' || v_emp_id);
ROLLBACK;
END;
/A DELETE statement can return values from the deleted row:
DECLARE
v_name emp_demo.emp_name%TYPE;
BEGIN
DELETE FROM emp_demo
WHERE emp_id = 3
RETURNING emp_name INTO v_name;
DBMS_OUTPUT.PUT_LINE('Deleted: ' || v_name);
ROLLBACK;
END;
/RETURNING INTO does not commit the transaction. You still choose COMMIT or ROLLBACK.
Exercise
Why might UPDATE ... RETURNING INTO fail when its WHERE clause matches several employees?
Show answer
A scalar variable can hold one returned value, but the statement produces several values. Use a unique condition for one row or use BULK COLLECT with a collection.