Question

Let's say I have a function within which I need to perform several actions, all of which depend upon the results of one query. Everything I've been able to find indicates that I need to define a temp table outside of the procedure, which I DO NOT WANT TO DO.

I'd like to do something like the following:

create or replace function f_example(
  a_input in number
)
return varchar2 is
begin
  create local temporary table tempIDs
  ( 
    testID number(6, 0)
    , testValue number(8,0)
  );

  //select data from tableFoo that will be overwritten by a_input into tempIDs

  //update data in tableFoo with a_input & store old data in another tableFoo field

end f_example;

This syntax doesn't work. Oracle doesn't allow 'Create' inside a function.

I'm not really a database programmer - I'm used to working in C# and Java. In that case I would store my values in a local array (or whatever) that goes out of scope when the method finished. Is there legitimately no way to do something like this in Oracle SQL?

Was it helpful?

Solution

You can define a PL/SQL record type and associated table type. Then you can issue a SELECT...BULK COLLECT to fill your table.

declare
  type my_record_type is record (testId number, testvalue number);
  type my_table_type is table of my_record_type index by binary_integer;
  my_table my_table_type;
begin
  select x, y bulk collect into my_table from table_foo;
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top