Description
It would be useful to allow access to DML with RETURNING
clauses in SELECT
. The SQL standard itself provides something called a "table function derived table " (see 7.6 <table reference> in SQL:2016-2), allowing you to access a table-valued function as a table. Given DML with RETURNING
could be considered a table-valued function, this syntax could be useful to provide access to DML in select.
Note that SQL standard also has something called a "data change delta table" (see also section 7.6 in SQL:2016-2), which is intended to do the same (providing access to the changes of a DML statement in a SELECT), but it comes with additional clauses which makes no sense for us, but essentially below proposal is a combination of "table function derived table " and "data change delta table".
My proposal is the follows:
<data-change-table> ::= TABLE '(' <modifying-dml> ')' [AS] <alias> [(<derived-column-list>)]
<modifying-dml> :: = { <insert> | <update-searched> | <update-or-insert> | <delete-searched> | <merge> }
<table-primary> ::= { ..existing options.. | <data-change-table> }
With the added requirement that the modifying-dml must have a RETURNING
clause. The data-change-table would be similar to a derived-table.
It should be possible to use columns of joined tables as parameters, similar to using selectable stored procedures, but we may need to consider if that should require explicit use of LATERAL
or not.
We could also consider extending the same treatment to EXECUTE BLOCK
with RETURNS
, and maybe EXECUTE PROCEDURE
, but that might be something for later.
Alternatives:
- The requirement that a statement must have
RETURNING
could also be replaced with providing an empty table stream if theRETURNING
clause is absent. This might not seem very useful, but will allow calling a DML statement from within a select, even if you're not interested in the result.