Home » SQL & PL/SQL » SQL & PL/SQL » Doubt with Associative Arrays (Oracle 9i, Solaris 10 Operating System)
Doubt with Associative Arrays [message #610372] Wed, 19 March 2014 10:46 Go to next message
akull
Messages: 46
Registered: July 2012
Location: Argentina
Member
Hi Experts!
I have a doubt regarding Associative Arrays in Oracle. Here is my Code


DECLARE

TYPE R_WA IS RECORD
(
v_id        au.id%type,
v_usuario   au.user%type,
v_ticket    au.ticket%type
);

TYPE T_WA IS TABLE OF R_WA;

V_WA T_WA;

BEGIN

select id, user, ticket
bulk collect into v_wa
from au;

END;



in this part of the code, if I select id, user and ticket number from my table it throws an Error


select id, user, ticket
into v_wa
from au;



but if I select them using bulk collect it doesn't throw any exceptions and works fine.


select id, user, ticket
bulk collect into v_wa
from au;



Hope you can help me, I checked all regarding documents from docs.oracle and also I checked the manuals from the course Oracle Database: PL/SQL Fundamentals and Advanced both of them from Oracle University.

Thanks in advance.
Steve.
Re: Doubt with Associative Arrays [message #610374 is a reply to message #610372] Wed, 19 March 2014 11:05 Go to previous messageGo to next message
ThomasG
Messages: 3211
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
You bulk collect *multiple* rows into a table type.

If you only have *one* row, you can't select it into the table type (T_WA), you have to select it into the record type (R_WA).

[Updated on: Wed, 19 March 2014 11:07]

Report message to a moderator

Re: Doubt with Associative Arrays [message #610378 is a reply to message #610374] Wed, 19 March 2014 11:47 Go to previous messageGo to next message
akull
Messages: 46
Registered: July 2012
Location: Argentina
Member
Thomas,
thank you for replying. But still I do not get the idea. For instance, if I restrict the search and add a where clause making sure that the resultset returned by the query has only one record if I bulk collect it, even one single record, it is added to the varible T_WA (type of V_WA)

It might be something from the very inside of the Engine.

Thank you again!

Steve!
Re: Doubt with Associative Arrays [message #610379 is a reply to message #610374] Wed, 19 March 2014 12:05 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
ThomasG wrote on Wed, 19 March 2014 12:05
You bulk collect *multiple* rows into a table type.

If you only have *one* row, you can't select it into the table type (T_WA), you have to select it into the record type (R_WA).


Not sure what you mean. It doesn't make much sense to bulk collect 1 row, but it is perfectly valid:

DECLARE
    TYPE R_WA IS RECORD(
                        v_id        emp.empno%type,
                        v_usuario   emp.ename%type,
                        v_ticket    emp.job%type
                       );
    TYPE T_WA IS TABLE OF R_WA;
    V_WA T_WA;
BEGIN
    select  empno,
            ename,
            job
      bulk  collect
      into  v_wa
      from  emp
      where rownum = 1;
END;
/

PL/SQL procedure successfully completed.

SCOTT@orcl > 


What OP istalking about is:

SCOTT@orcl > DECLARE
  2      TYPE R_WA IS RECORD(
  3                          v_id        emp.empno%type,
  4                          v_usuario   emp.ename%type,
  5                          v_ticket    emp.job%type
  6                         );
  7      TYPE T_WA IS TABLE OF R_WA;
  8      V_WA T_WA;
  9  BEGIN
 10      select  empno,
 11              ename,
 12              job
 13  --      bulk  collect
 14        into  v_wa
 15        from  emp;
 16  END;
 17  /
      into  v_wa
            *
ERROR at line 14:
ORA-06550: line 14, column 13:
PLS-00642: local collection types not allowed in SQL statements
ORA-06550: line 15, column 7:
PL/SQL: ORA-00947: not enough values
ORA-06550: line 10, column 5:
PL/SQL: SQL Statement ignored


SCOTT@orcl >  


Query returns record(s). Bulk collect takes each record and adds it to collection. Without bulk collect we end up with a regular situation where fetched record is assigned to record variable listed in into clause or fetched record attributes are assigned to variables listed in into clause. And in our case re have a collection listed in into clause. So oracle tries to do type R_WA conversion to T_WA. Obviosly scalar record can't be assigned to collection. But first Oracle realizes T_WA is not SQL type and throws PLS-00642.

SY.

[Updated on: Wed, 19 March 2014 12:06]

Report message to a moderator

Re: Doubt with Associative Arrays [message #610380 is a reply to message #610378] Wed, 19 March 2014 12:05 Go to previous messageGo to next message
ThomasG
Messages: 3211
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
Well, a table (type) is a table and a record (type) is a record. They are two fundamentally different things.

Like a box and an egg are two fundamentally different things. Even when you have only one egg in a box, that doesn't magically turn the box into an egg.
Re: Doubt with Associative Arrays [message #610381 is a reply to message #610380] Wed, 19 March 2014 12:10 Go to previous messageGo to next message
akull
Messages: 46
Registered: July 2012
Location: Argentina
Member
Thank you very Much guys!

Regards!
Steve!
Re: Doubt with Associative Arrays [message #610382 is a reply to message #610380] Wed, 19 March 2014 12:14 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
It doesn't matter if you have one egg or a dozen. We have a box but we don't know how to put eggs into that box. Bulk collect into knows how, but plain into does not. Plain into only knows to give us one egg. It will be confused when there is more than one egg or we give it a box to put that egg into while it is expecting a single place for that single egg or if that single place it was given is not a place for an egg. Bulk knows it needs to be given a box to put egg(s) into it. It will be confused if it doesn't get a box even though there is only one egg. And similar to plain into will be also confused if box it got is not a box for eggs.

SY.

[Updated on: Wed, 19 March 2014 12:23]

Report message to a moderator

Re: Doubt with Associative Arrays [message #610389 is a reply to message #610382] Wed, 19 March 2014 13:02 Go to previous messageGo to next message
akull
Messages: 46
Registered: July 2012
Location: Argentina
Member
Thank you very much SY!

Regards,
Steve
Re: Doubt with Associative Arrays [message #610407 is a reply to message #610379] Wed, 19 March 2014 17:15 Go to previous message
ThomasG
Messages: 3211
Registered: April 2005
Location: Heilbronn, Germany
Senior Member
Solomon Yakobson wrote on Wed, 19 March 2014 18:05

Not sure what you mean. It doesn't make much sense to bulk collect 1 row, but it is perfectly valid:


Well, I meant you can't "bulk collect" into a record, only into a table type. And the other way around you can't "select into" a table type, only into a record.

But the OP got it by now I guess, with your example. Wink
Previous Topic: Replace Date column to Varchar Value
Next Topic: Writing SQL Query
Goto Forum:
  


Current Time: Wed Apr 24 13:24:00 CDT 2024