Data manipulation in enterprise Oracle involves atomic batch operations, deterministic concurrency control, and zero-race-condition UPSERT logic using the standard MERGE statement.
The MERGE Statement (Atomic UPSERT)
In distributed architectures, attempting to perform a SELECT, checking if a record exists in application code, and then executing either an INSERT or UPDATE introduces severe race conditions and unnecessary network latency.
Oracle's MERGE statement executes conditional insert/update logic inside a single atomic database engine call:
MERGE INTO inventory_levels target
USING (
SELECT
4502 AS product_id,
'WAREHOUSE_EAST' AS location_code,
150 AS quantity_delta,
50 AS reorder_threshold
FROM DUAL
) source
ON (
target.product_id = source.product_id AND
target.location_code = source.location_code
)
WHEN MATCHED THEN
UPDATE SET
target.current_quantity = target.current_quantity + source.quantity_delta,
target.last_restocked = SYSTIMESTAMP
WHERE target.is_active = 1 -- Conditional update filter
WHEN NOT MATCHED THEN
INSERT (
product_id, location_code, current_quantity,
reorder_threshold, is_active, created_at
)
VALUES (
source.product_id, source.location_code, source.quantity_delta,
source.reorder_threshold, 1, SYSTIMESTAMP
);
The RETURNING INTO Clause
When an INSERT, UPDATE, or DELETE executes, application layers often require the generated primary key or calculated virtual values. Rather than making a second database query, use RETURNING:
-- Single-pass insertion with identity return in SQL*Plus or PL/SQL
VARIABLE v_new_order_id NUMBER;
VARIABLE v_created_timestamp VARCHAR2(50);
INSERT INTO customer_orders (customer_id, order_total, status)
VALUES (10042, 2450.75, 'PENDING')
RETURNING order_id, TO_CHAR(created_at, 'YYYY-MM-DD HH24:MI:SS')
INTO :v_new_order_id, :v_created_timestamp;
Multi-Table Inserts: Conditional and Unconditional
Oracle enables writing to multiple destination tables in a single scan of the source data:
-- Unconditional multi-table insert
INSERT ALL
INTO audit_events (event_name, logged_at) VALUES ('ORDER_COMPLETED', SYSTIMESTAMP)
INTO ledger_transactions (order_id, amount) VALUES (order_num, total_amt)
SELECT order_num, total_amt FROM staging_orders;
-- Conditional multi-table insert (INSERT FIRST stops at first match; INSERT ALL evaluates all)
INSERT FIRST
WHEN total_amt >= 10000 THEN
INTO high_value_orders (order_id, amount) VALUES (order_num, total_amt)
WHEN total_amt >= 1000 THEN
INTO standard_orders (order_id, amount) VALUES (order_num, total_amt)
ELSE
INTO low_value_orders (order_id, amount) VALUES (order_num, total_amt)
SELECT order_num, total_amt FROM daily_orders;
Transaction Isolation & Savepoints
Oracle enforces Statement-Level Read Consistency by default using Multi-Version Concurrency Control (MVCC) powered by Undo Segments. Readers never block writers, and writers never block readers.
BEGIN
-- Step 1: Deduct balance
UPDATE user_accounts
SET balance = balance - 500
WHERE account_id = 101;
-- Set a rollback checkpoint
SAVEPOINT step_one_complete;
-- Step 2: Attempt secondary charge
UPDATE user_accounts
SET balance = balance - 100
WHERE account_id = 999; -- Imagine this account is invalid
-- Rollback strictly to checkpoint without aborting step 1
ROLLBACK TO SAVEPOINT step_one_complete;
-- Persist step 1
COMMIT;
END;
/
Exercises
- Write an atomic
MERGEstatement that updates an employee's salary and department if theiremailmatches, or creates a new employee row if not found. - Demonstrate how
RETURNINGretrieves a sequence-generated identity ID and sysdate in a single round-trip. - What is the difference between
INSERT ALLandINSERT FIRSTwhen multipleWHENconditions evaluate to true?