Solved

Oracle Collections

Posted on 2006-07-10
1
401 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
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
This article describes some very basic things about SQL Server filegroups.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
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…

744 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now