UPDATE with FROM and RETURNING
Performs UPDATE with a FROM clause to join additional tables and uses RETURNING to retrieve modified rows for verification and auditing.
Code
UPDATE products p
SET
unit_price = p.unit_price * (1 + adj.price_adjustment),
updated_at = CURRENT_TIMESTAMP,
updated_by = current_user
FROM price_adjustments adj
WHERE p.product_id = adj.product_id
AND adj.effective_date <= CURRENT_DATE
AND adj.is_active = true
RETURNING
p.product_id,
p.product_name,
p.unit_price AS new_price,
(p.unit_price / (1 + adj.price_adjustment)) AS old_price,
adj.price_adjustment AS adjustment;Report a problem with this exampleOpens a pre-filled issue on GitHub.