Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert data into TABLE type variable

I'm expecting a lot of downvotes, since I think this is a silly question, but here it goes:

I'm attempting at inserting data into a TABLE TYPE variable. I have a pre-existing TABLE TYPE, defined as:

create or replace type m_code_object
 is object (m_code varchar2(25 char));

create or replace type m_code_tabletype
 is table of m_code_object;

and the table that I want to define based on this and insert data into, is below:

declare
    vtable m_code_tabletype;
begin
    insert into vtable values ('a');
    insert into vtable values ('b');
end;

Now, when running this in SQL Developer I get PL/SQL: ORA-00942: table or view does not exist for both rows.

My understanding was that this is a table variable of type m_code_tabletype and that it's enough to declare it in a block before trying to insert data into it.

Can anyone please explain what I'm doing wrong or what's missing from my understanding?

Thanks

like image 824
Radu Gheorghiu Avatar asked Aug 25 '15 13:08

Radu Gheorghiu


People also ask

How do you populate a table type based on an object type?

It really is an array *of objects*. So if we want to populate that array, the elements inside it must be *objects*. SQL> CREATE OR REPLACE TYPE T1_OBJ AS OBJECT 2 (field1 VARCHAR2(10), 3 field2 INTEGER); 4 / Type created. SQL> SQL> CREATE OR REPLACE TYPE T1_TAB AS TABLE OF T1_OBJ; 2 / Type created.

How do you declare a variable with %type attribute?

Using the %TYPE Attribute to Declare Variables For example, suppose you want to declare variables as the same datatype as the employee_id and last_name columns in employees table. To declare variables named empid and emplname that have the same datatype as the table columns, use dot notation and the %TYPE attribute.

What is the use of %type in PL SQL?

The %TYPE attribute lets use the datatype of a field, record, nested table, database column, or variable in your own declarations, rather than hardcoding the type names. You can use the %TYPE attribute as a datatype specifier when declaring constants, variables, fields, and parameters.


2 Answers

This is how you populate a table type of objects :

declare
    vtable m_code_tabletype := m_code_tabletype();
begin
    vtable.extend;
    vtable(vtable.count) := m_code_object('a');
    vtable.extend;
    vtable(vtable.count) := m_code_object('b');
end;
like image 60
atik Avatar answered Oct 19 '22 22:10

atik


DECLARE
    c_varray SYS.ODCIVARCHAR2LIST;
BEGIN
    c_varray := SYS.ODCIVARCHAR2LIST();
    c_varray.EXTEND(2);
    c_varray(1) := '1';
    c_varray(2) := '2';
END;

Or

DECLARE
    c_varray SYS.ODCIVARCHAR2LIST;
BEGIN
    SELECT LEVEL BULK COLLECT INTO c_varray FROM DUAL CONNECT BY LEVEL <= 10;
END;
like image 1
Husqvik Avatar answered Oct 19 '22 21:10

Husqvik