Select into a temporary table in Oracle

Sait picture Sait · Feb 22, 2015 · Viewed 85.2k times · Source

I am trying to do something like the following,

select * into temp from (select * from student);

It gives me the following error,

ERROR at line 1:
ORA-00905: missing keyword

In my real example the subquery (select * from student) is more complex.

I want to use this in a stored procedure, so I don't want to create the table itself. I just want to make my code more readable by using a temp table.

Answer

Martina picture Martina · Feb 22, 2015

Then perhaps you need to do something like this:

declare
   type t_temp_storage is table of student%rowtype;
   my_temp_storage t_temp_storage;
begin
   select * bulk collect into my_temp_storage from student;
   for i in 1..my_temp_storage.count
    loop
    dbms_output.put_line('here I am '||my_temp_storage(i).stuid);
   end loop; 
 end;