RETURNING clause
INSERT, REPLACE and DELETE ... RETURNING return the rows they wrote or removed as a result set (MariaDB form).
RETURNING makes a write statement answer with a result set of the rows it wrote or removed (MariaDB's feature, D81). The parser accepts it after INSERT, REPLACE and DELETE without testing the dialect; the behaviour is verified against MariaDB 11.4. MySQL has no RETURNING.
Syntax#
INSERT ... [RETURNING select_item [, select_item] ...]
REPLACE ... [RETURNING select_item [, select_item] ...]
DELETE FROM tbl_name [WHERE ...] [ORDER BY ...] [LIMIT n] [RETURNING select_item [, select_item] ...]
select_item:
* | expr [[AS] alias]Arguments#
| Name | Meaning | Default |
|---|---|---|
select_item | Columns or expressions of the written (or removed) row; * is all visible columns. | none |
Remarks#
- The items are checked before anything is written: an unknown column fails the statement with nothing changed.
- The rows are those the statement wrote or removed. For
INSERT ... SELECTand multi-rowVALUESthere is one result row per written row. INSERT IGNORE ... RETURNINGgives back the auto-increment value of a row thatIGNOREleft out, as MariaDB does.REPLACE ... RETURNINGreturns the new rows.UPDATE ... RETURNINGis not in the grammar (MariaDB 11.4 does not have it either).- The privileges are those of the write statement.
Examples#
Example:
INSERT INTO t (name, qty) VALUES ('a', 1), ('b', 2), ('c', 3) RETURNING id, name;
INSERT INTO t (name) VALUES ('d') RETURNING *;
INSERT INTO t (name, qty) VALUES ('e', 7) RETURNING id AS new_id, qty * 2 AS doubled, CONCAT(name, '!') AS shouted;
DELETE FROM t WHERE id IN (2, 3) RETURNING id, name;
DELETE FROM t ORDER BY id LIMIT 2 RETURNING id;