Fill table of custom type and use it in for loop to insert : Table of Type « PL SQL « Oracle PL / SQL






Fill table of custom type and use it in for loop to insert

    

SQL>
SQL> CREATE TABLE MyTable (
  2    num_col    NUMBER,
  3    char_col   VARCHAR2(60)
  4    );

Table created.

SQL>
SQL> DECLARE
  2    TYPE t_Numbers IS TABLE OF MyTable.num_col%TYPE
  3      INDEX BY BINARY_INTEGER;
  4    TYPE t_Chars IS TABLE OF MyTable.char_col%TYPE
  5      INDEX BY BINARY_INTEGER;
  6    v_Numbers t_Numbers;
  7    v_Chars t_Chars;
  8  BEGIN
  9    FOR v_Count IN 1..50 LOOP
 10      v_Numbers(v_Count) := v_Count;
 11      v_Chars(v_Count) := 'Row number ' || v_Count;
 12    END LOOP;
 13
 14    FOR v_Count IN 1..50 LOOP
 15      INSERT INTO MyTable VALUES
 16        (v_Numbers(v_Count), v_Chars(v_Count));
 17    END LOOP;
 18  END;
 19  /

PL/SQL procedure successfully completed.

SQL>
SQL> select * from MyTable;

 NUM_COL CHAR_COL
-------- ------------------------------------------------------------
    1.00 Row number 1
    2.00 Row number 2
    3.00 Row number 3
    4.00 Row number 4
    5.00 Row number 5
    6.00 Row number 6
    7.00 Row number 7
    8.00 Row number 8
    9.00 Row number 9
   10.00 Row number 10
   11.00 Row number 11

 NUM_COL CHAR_COL
-------- ------------------------------------------------------------
   12.00 Row number 12
   13.00 Row number 13
   14.00 Row number 14
   15.00 Row number 15
   16.00 Row number 16
   17.00 Row number 17
   18.00 Row number 18
   19.00 Row number 19
   20.00 Row number 20
   21.00 Row number 21
   22.00 Row number 22

 NUM_COL CHAR_COL
-------- ------------------------------------------------------------
   23.00 Row number 23
   24.00 Row number 24
   25.00 Row number 25
   26.00 Row number 26
   27.00 Row number 27
   28.00 Row number 28
   29.00 Row number 29
   30.00 Row number 30
   31.00 Row number 31
   32.00 Row number 32
   33.00 Row number 33

 NUM_COL CHAR_COL
-------- ------------------------------------------------------------
   34.00 Row number 34
   35.00 Row number 35
   36.00 Row number 36
   37.00 Row number 37
   38.00 Row number 38
   39.00 Row number 39
   40.00 Row number 40
   41.00 Row number 41
   42.00 Row number 42
   43.00 Row number 43
   44.00 Row number 44

 NUM_COL CHAR_COL
-------- ------------------------------------------------------------
   45.00 Row number 45
   46.00 Row number 46
   47.00 Row number 47
   48.00 Row number 48
   49.00 Row number 49
   50.00 Row number 50

50 rows selected.

SQL>
SQL>
SQL>
SQL> drop table MyTable;

Table dropped.

SQL>
SQL>

   
    
    
  








Related examples in the same category

1.Create a function to convert string type variable to date type variable
2.Fetch a bulk into a table structure
3.Define a nested table type for each column
4.Extend once, outside the loop for better performance
5.A package to manage a list of employees
6.Table of custome type indexed by BINARY_INTEGER
7.Reference type attribute through index
8.Use for loop to fill a table collection
9.Select bulk collect into
10.Use for loop to insert value to table collection and then use table collection in another insert statement
11.The EXISTS Table Attribute
12.FIRST & LAST Table Attributes
13.NEXT & PRIOR Table Attributes
14.Uses the COUNT method to display the number of rows contained in a table collection
15.How to do a bulk collect into a nested table.
16.How to do a bulk collect into an associative array
17.Table collection indexed by column type