Solved

Oracle Collections

Posted on 2006-07-10
1
455 Views
Last Modified: 2010-08-05
Hi there:

I am using Oracle 10g on windows;

I have a table called books the description of the table is:

SQL> desc books;
 
Name                                       Null?    Type
----------------                           -------   ------------
ISBN                                        NOT NULL VARCHAR2(13)
TITLE                                                      VARCHAR2(200)    
SUMMARY                                               VARCHAR2(2000)
AUTHOR                                                  VARCHAR2(200)
DATE_PUBLISHED                                     DATE
PAGE_COUNT                                           NUMBER


The values of PAGE_COUNT column in books table are as follows:

SQL> select page_count from books;

PAGE_COUNT
----------
       420
       401
       502
        94
       158
       120
       692
       942
       544
       588

10 rows selected.



When I run the following query I bet the following result, I do not why, please help. THANKS!

SQL> Declare
  2    type isbn_list is table of varchar2(13);
  3    my_books isbn_list
  4    := isbn_list(
  5            '1-56592-375-8',
  6            '0-596-00121-5',
  7            '1-56592-849-0',
  8            '1-56592-335-9',
  9            '1-56592-674-9',
 10            '1-56592-675-7',
 11            '0-596-00180-0',
 12            '1-56592-457-6'
 13       );
 14  
 15  type page_numbers is table of books.page_count%type
 16  index by binary_integer;
 17  pnos page_numbers;
 18  
 19  Begin
 20     Forall book_index In
 21            my_books.First..my_books.Last
 22            update books
 23            set page_count = pnos(book_index)
 24            where isbn     = my_books(book_index);
 25     If SQL%ROWCOUNT != 8
 26     Then
 27        DBMS_OUTPUT.PUT_LINE( 'We are missing a book!');
 28     End if;
 29  
 30     If SQL%BULK_ROWCOUNT(4) = 0
 31     Then
 32        DBMS_OUTPUT.PUT_LINE( 'What happened to Oracle PL/SQL Programming?');
 33     End if;
 34  End;
 35  /
Declare
*
ERROR at line 1:
ORA-22160: element at index [1] does not exist
ORA-06512: at line 21


WHEN I hard code the value of  pnos(book_index) and substitue it say 144 the same query works fine as follows:
(SEE LINE NUMBER 23 BELOW)


SQL>  Declare
  2     type isbn_list is table of varchar2(13);
  3     my_books isbn_list
  4     := isbn_list(
  5             '1-56592-375-8',
  6             '0-596-00121-5',
  7             '1-56592-849-0',
  8             '1-56592-335-9',
  9             '1-56592-674-9',
 10             '1-56592-675-7',
 11             '0-596-00180-0',
 12             '1-56592-457-6'
 13        );
 14  
 15   type page_numbers is table of books.page_count%type
 16   index by binary_integer;
 17   pnos page_numbers;
 18  
 19   Begin
 20      Forall book_index In
 21             my_books.First..my_books.Last
 22             update books
 23             set page_count = 144
 24            where isbn     = my_books(book_index);
 25     If SQL%ROWCOUNT != 8
 26     Then
 27        DBMS_OUTPUT.PUT_LINE( 'We are missing a book!');
 28     End if;
 29  
 30     If SQL%BULK_ROWCOUNT(4) = 0
 31     Then
 32        DBMS_OUTPUT.PUT_LINE( 'What happened to Oracle PL/SQL Programming?');
 33     End if;
 34  End;
 35  /

PL/SQL procedure successfully completed.

SQL>



SQL> select page_count from books;

PAGE_COUNT
----------
       420
       144
       502
       144
       158
       120
       692
       942
       544
       144

10 rows selected.


Thanks

Muhammad Riyaz
0
Comment
Question by:blf2vkr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 25

Accepted Solution

by:
jrb1 earned 125 total points
ID: 17073686
Hi blf2vkr,

You have created a table in memory called pnos.  However, you are never loading this memory table with data from the books table.  

It should looks like this:

Begin
   select page_count BULK COLLECT into pnos from books;

    Forall book_index In


Regards,
John
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Azure Functions is a solution for easily running small pieces of code, or "functions," in the cloud. This article shows how to create one of these functions to write directly to Azure Table Storage.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question