Oracle equivalent of PostgreSQL INSERT...RETURNING *;
It's not currently possible, especially in an old version of Oracle such as 8i. See this answer to a similar question.
Oracle SQL inserting multiple rows and returning something
From the documentation (at least through the 21c version), one of the restrictions to the returning clause:
You cannot specify the
returning_clause for a multitable
insert.
Copying table values with dependent child records
The usual way to insert a row and get the generated sequence value in a single statement is to use the RETURNING INTO
clause. Unfortunately, this clause is not supported with the multi-row INSERT SELECT
statement (as of 11.2.0.2.0).
So you will have to either:
use a cursor and insert in the parent table row-by-row (then use one multi-row insert in the child table for each row in the parent table).
For instance, in your case:
DECLARE
l_key aparent.aparent_code%TYPE;
BEGIN
FOR cc IN (SELECT a.aparent_code, a.value1, a.value2
FROM aparent a
WHERE a.avalue1 = 10) LOOP
INSERT INTO aparent
VALUES (sq_aparent.nextval, cc.value1, cc.value2)
RETURNING aparent_code INTO l_key; -- get the new parent key
INSERT INTO achild
(SELECT sq_achild.nextval, l_key, value3 -- use the new parent key
FROM achild
WHERE aparent_code = cc.aparent_code);
END LOOP;
END;- use multi-row inserts with a workaround as you did.
I think your approach may be the most efficient generally, especially if many parent rows are being copied. The overhead of inserting into the temporary table and joining to it should be minimal compared to the large overhead incurred by many single-row statements.
View Top N-Results in Oracle
Is this what you want?
select *
from (select product_name, (qty_on_order - qty_on_hand) as Sales
from products join
product_inventory
on product_inventory.product_id = products.product_id
order by Sales Desc
) p
where rownum <= 3;
Related Topics
SQL Pivot with Multiple Columns
Rails Union Hack, How to Pull Two Different Queries Together
If() Statement Alternative in SQLite
Cannot Select from Update Returning Clause in Postgres
Running Total by Grouped Records in Table
How to Create an Index for Elements of an Array in Postgresql
How to Design a Schema Where the Columns of a Table Are Not Fixed
Add Unique Constraint in SQL Server 2008 Gui
SQL Datediff - Find Datediff Between Rows
Sql: Sum 3 Columns When One Column Has a Null Value
MySQL Bulk Load Command Line Tool
How to Properly Add Brackets to SQL Queries with 'Or' and 'And' Clauses by Using Arel
Writing a Subquery Using Zend Db
SQL Query with Distinct and Sum
Return All Possible Combinations of Values Within a Single Column in SQL
How to Find Left Outer Join or Right Outer Join with Oracle Join (+)
SQL Server - Possible Pivot Solution
Conversion Failed When Converting the Nvarchar Value ... to Data Type Int