Example 1
Apply a price increase and get the updated row back
The UPDATE reduces Widget's stock from 100 to 90. With RETURNING product_id, product_name, stock, the engine returns the post-update row values in the same response, so the application does not need a follow-up SELECT. SQL Server's OUTPUT INSERTED.col syntax achieves the same result using the INSERTED pseudo-table, which holds the new row state after the update. In an UPDATE, DELETED holds the old values and INSERTED holds the new values, making it possible to return both old and new state in a single OUTPUT clause.
CREATE TABLE inventory (
product_id INT,
product_name VARCHAR(50),
stock INT
);
INSERT INTO
inventory
VALUES
(1, 'Widget', 100),
(2, 'Gadget', 50),
(3, 'Doohickey', 75);UPDATE inventory
SET
stock = stock - 10
WHERE
product_id = 1 RETURNING product_id,
product_name,
stock;| product_id | product_name | stock |
|---|---|---|
| 1 | Widget | 90 |
CREATE TABLE inventory (
product_id INT,
product_name VARCHAR(50),
stock INT
);
INSERT INTO
inventory
VALUES
(1, 'Widget', 100),
(2, 'Gadget', 50),
(3, 'Doohickey', 75);UPDATE inventory
SET
stock = stock - 10 OUTPUT INSERTED.product_id,
INSERTED.product_name,
INSERTED.stock
WHERE
product_id = 1;| product_id | product_name | stock |
|---|---|---|
| 1 | Widget | 90 |
PostgreSQL, MariaDB, and SQLite use `RETURNING`. SQL Server uses `OUTPUT INSERTED.*`. MySQL has no equivalent.