In Oracle, you can update a table using the result of a SELECT statement through several methods. Here are the primary approaches, along with examples and key considerations:
1. Correlated Subquery in SET Clause
Update columns using a subquery that references the main table.
sql
UPDATE target_table t
SET (t.column1, t.column2) = (
SELECT s.source_col1, s.source_col2
FROM source_table s
WHERE s.join_key = t.join_key -- Correlate subquery with main table
AND ROWNUM = 1 -- Ensure only 1 row is returned
)
WHERE EXISTS (
SELECT 1
FROM source_table s
WHERE s.join_key = t.join_key
);
Notes:
Use ROWNUM = 1 if the subquery might return multiple rows (to avoid ORA-01427).
WHERE EXISTS prevents updating rows to NULL if no match exists.
2. MERGE Statement (Recommended for Complex Logic)
Combine update/insert operations in a single statement.
sql
MERGE INTO target_table t
USING (
SELECT join_key, source_col1, source_col2
FROM source_table
WHERE [conditions] -- Optional filtering
) s
ON (t.join_key = s.join_key) -- Join condition
WHEN MATCHED THEN
UPDATE
SET t.column1 = s.source_col1,
t.column2 = s.source_col2;
Notes:
Efficient for large datasets.
Add WHERE clauses in the USING subquery or UPDATE to filter rows.
Omit the WHEN NOT MATCHED clause if inserts are not needed.
3. Inline View with ROWID
Use an updatable inline view (requires ROWID for direct row access).
sql
UPDATE (
SELECT
t.column1 AS target_col1,
t.column2 AS target_col2,
s.source_col1,
s.source_col2
FROM target_table t
JOIN source_table s ON t.join_key = s.join_key
)
SET target_col1 = source_col1,
target_col2 = source_col2;
Notes:
The view must be key-preserved (each row in the view maps to exactly one row in the target table).
Avoid if the join might produce duplicate rows for the target table (causes ORA-01779).
4. PL/SQL Loop (Row-by-Row Processing)
Use a cursor loop for procedural control (least efficient for bulk operations).
sql
BEGIN
FOR rec IN (
SELECT t.rowid AS target_rowid, s.source_col1, s.source_col2
FROM target_table t
JOIN source_table s ON t.join_key = s.join_key
WHERE [conditions]
) LOOP
UPDATE target_table t
SET column1 = rec.source_col1,
column2 = rec.source_col2
WHERE t.rowid = rec.target_rowid;
END LOOP;
END;
/
Notes:
Only use for small datasets due to performance overhead.
ROWID ensures direct row access without ambiguity.
Key Considerations
Uniqueness:
Subqueries in SET must return exactly one row per target row (use ROWNUM, MAX(), or DISTINCT if needed).
Performance:
For bulk updates, MERGE is typically fastest.
Add indexes on join columns to optimize subqueries.
Locking:
Updates lock affected rows; minimize transaction time for large operations.
Alternatives:
Use CREATE TABLE AS SELECT if replacing the entire table is feasible.
Temporary tables/staging tables can simplify multi-step updates.
Example Scenario
Update employee salaries from a department bonus table:
sql
-- Using MERGE
MERGE INTO employees e
USING dept_bonus d
ON (e.dept_id = d.dept_id)
WHEN MATCHED THEN
UPDATE SET e.salary = e.salary + d.bonus;
Choose the method that best fits your data volume, uniqueness constraints, and performance requirements. For most cases, MERGE is optimal.