Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert result of select statement to another table in Oracle stored procedure

I am preparing a stored procedure in oracle. In this procedure I have a select statement which will result in multiple rows. Now I want to insert these rows to another table.

Please someone tell me it is really urgent.

UPDATE:

One thing that I forgot to mention is that I also have to add another column in my result of select statement. This column will contain value of a parameter in this stored procedure.

Below is the sample stored procedure for this

Create or Replace PROCEDURE "My Procedure" 
  (
    my_id in number,
    nric in VARCHAR2
  )
BEGIN
insert into new_table(my_new_id,field1, field2)
select my_id,table1.field1, table2.field2 
from table1, table2 
where table1.age=table2.age AND table1.my_nric=nric;
END

In above sample I have my_id in procedure parameter and want to insert that in new_table for each result entry from table1 and table2.

like image 960
Nikhil Gaur Avatar asked Dec 20 '22 18:12

Nikhil Gaur


1 Answers

You can try this

INSERT INTO myTable(field1, field2, field3)
   SELECT field1, field2, field3
     FROM anotherTable
       WHERE thisCondition = 'mycondition';
like image 160
codingbiz Avatar answered May 03 '23 04:05

codingbiz