Given a table T(x, y, z, t, u, v, ...) is it possible in Oracle to write this query without listing all columns (be it in the SELECT or in the INSERT part)?
INSERT INTO T (x, y, z, t, u, v, ...)
SELECT 'new', y, z, t, u, v, ...
FROM T
WHERE x = 'old'
The effect is that all rows for which x has the value of old are duplicated, except that now x has the value of new.
No. The only way to avoid typing an explicit projection is to use all the table’s columns. You aren’t doing that, because you want to use a literal instead of column
X. That means you have to list all the other columns in the SELECT projection.Of course, you don’t have to specify the columns in the INSERT clause.
Over the years developers have occasionally wished for an “except” syntax, something like:
but it’s never made it into the ANSI standard. In fact, I doubt if it’s even been discussed.
Okay, here is a proof of concept which uses the data dictionary to produce a dynamic insert statement.
It makes the following assumptions:
You will need to adjust the code if any of those assumptions are wrong.
The procedure loops round the USER_TAB_COLUMNS table, sorting the columns into the table’s projected order. It concatenates the column names into the SELECT clause of an INSERT statement, except where the name is that of the substituted column when it concatenates the provided literal instead. Finally it uses Native Dynamic SQL to run the assembled INSERT statement.