When you just look at the Oracle INSERT syntax, I noticed that you can insert into a query, for example.
insert into (select * from dept) (deptno, dname) values (99, 'new department');
Can anyone shed light on what this is for? What can I achieve by inserting into a query that I cannot directly attach to a table?
UPDATE: So far this seems like just an alternative syntax, so I can write
insert into (select deptno, dname from dept) values (99, 'new department');
instead
insert into dept (deptno, dname) values (99, 'new department');
The same thing, the same implementation plan. It doesn't matter if the query returns a record or not. It:
insert into (select deptno, dname from dept where 1 = 0) values (99, 'new department');
again leads to the same execution plan. Thus, we can assume that it really does not matter what the subquery looks like, as long as we select only the columns from the same table. But no. It:
insert into (select deptno, dname from dept cross join some_table) values (99, 'new department');
results in "ORA-01779: cannot change a column that maps to a table containing no keys" or "ORA-01732: the data manipulation operation is not legal in this view."
I got the impression that Oracle decided to allow the insertion of a query because they allow you to embed in views, why is the subquery here an ad hoc declaration? Therefore, when you can embed in a view, they, of course, allow you to embed an hoc representation in ad, but no one in their right mind would ever use this syntax, of course :-)
But maybe I'm wrong? Maybe this syntax offers something that I don't know yet? If so tell me :-)