Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to populate data into a nested table in Oracle when the nested table is within a record type

Tags:

oracle

plsql

I need to be able to return from a procedure a list of values in the form of a cursor variable. But within the list some fields can have multiple values

e.g. a product can have multiple description lines in the description field (obtained from a different table).

I was thinking in line of creating a nested table within a record type and associate this to a cursor.

TYPE N_TYPE IS TABLE OF VARCHAR2(350);
TYPE TYPE1 IS RECORD ( FIELD_1 VARCHAR2(100)
                      , FIELD_2 VARCHAR2(30)
                      , FIELD_3 N_TYPE);

TYPE T_CUR IS REF CURSOR RETURN TYPE1;

Procedure p_proc (p_1 IN VARCHAR2, p_2 OUT t_cur) is
  -- processing input parameter and passing out a cursor to host application
end p_proc;

Here within the procedure I will need to pass p_1 into a table and using a explicit cursor to retrieve the data into Field_1 and Field_2.

Then from another table I will need to assign multiple records into Field_3.

Can anyone show me how to populate data into a nested table when the table is part of a datatype within a record? And how do I check once it has been populated. And how to assign this back to a cursor variable for the out parameter?

like image 718
one23 Avatar asked Feb 16 '23 16:02

one23


1 Answers

This document: http://docs.oracle.com/cd/E11882_01/appdev.112/e25519/composites.htm#CIHIEBJC
describes how to work with collection types in PL/SQL:

Basic example:

DECLARE
   TYPE N_TYPE IS TABLE OF VARCHAR2(350);
   TYPE TYPE1 IS RECORD ( FIELD_1 VARCHAR2(100)
                      , FIELD_2 VARCHAR2(30)
                      , FIELD_3 N_TYPE);
   v_n n_type;
   v_type1 type1;
BEGIN
   v_n := n_type(); -- initialize an empty collection
   v_n.extend( 3 ); -- add 3 elements to the table
   v_n( 1 ) := 'First string ';
   v_n( 2 ) := 'Second string ';
   v_n( 3 ) := 'Third string ';
   v_n.extend; -- add 1 element at the end of the table
   v_n( v_n.last ) := 'Next string';

   --assign the table to the field_3 of the record
   v_type1.field_3 := v_n;

   -- check values
   FOR i in v_type1.field_3.first .. v_type1.field_3.last LOOP
      DBMS_OUTPUT.PUT_LINE(  v_type1.field_3( i ) );
   END LOOP;
END;
/

--- DBMS_OUTPUT -------
First string 
Second string 
Third string 
Next string
like image 103
krokodilko Avatar answered Feb 18 '23 10:02

krokodilko