Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

How do I create an heterogeneous iterable data structure in PL/SQL?

User_CTJPXNov 20 2020 — edited Nov 20 2020

I'm looking for a way to define a data structure in Oracle PL/SQL with heterogeneous data and where I'm able to iterate the values.
My use case is that I receive a bunch of values from a Resource Template and have to process them and insert them in a table.
I would like to have something like this:

DECLARE
   TYPE books IS RECORD
      (title  varchar( 50) := myvar1,
      author  varchar( 50) := myvar2,
      subject varchar(100) := myvar3,
      book_id number       := myvar4);
BEGIN
  FOR item IN ('SELECT * FROM books')
  LOOP
    -- Here I would access each individual item key and value (title, author...)
    my_function(item.key, item.value)
  END LOOP;
END;

Note that in the example above I want to iterate the fields (columns), not records, there's a single record.
So I want something very similar to an object in Javascript:

var obj = {
  title: 'Romeo and Juliet',
  author: 'Some dude',
  subject: 'Romance',
  book_id: 332764
};

for (const prop in obj) {
  console.log(`obj.${prop} = ${obj[prop]}`);
}

This is for Oracle 19c.
Is there a way to achieve this in a simple way?
Note that I'm using RECORD in the example, but it could be something else.
This was also asked in StackOverflow, but there were no answers.

Comments
Post Details
Added on Nov 20 2020
3 comments
228 views