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
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.
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.
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.
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;
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With