Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: PL/SQL
Expertise: Intermediate
Jun 20, 2003

Insert Large Numbers of Rows into a Database Table with PL/SQL

Everybody knows that it takes a long time to insert a large number of records into a table. To improve the speed of insertion, you can use the PL/SWL loop FORALL.

In PL/SQL, use a cursor to fetch the rows from one table and insert them into the other table.

To reduce fetching time, do a BULK COLLECT INTO:


FETCH
<cursor name>
BULK COLLECT INTO
<table variable>
LIMIT <value>
To insert the fetched row, use the FORALL loop.

FORALL INDX IN <range1>..<range2> SAVE EXCEPTIONS
You can also handle Exceptions by using the SAVE EXCEPTIONS option in the FORALL loop. Here, the FORALL loop acts as a bulk insert. When it encounters an exception, it just saves it and continues with the next record.

To view the exceptions that occurred during the insert, include the following statement after the FORALL loop:


EXCEPTION
WHEN bulk_errors THEN
FOR indx IN 1 .. SQL%BULK_EXCEPTIONS.COUNT
LOOP
errno := SQL%BULK_EXCEPTIONS(indx).ERROR_INDEX;
errmsg := SQLERRM(-1 * SQL%BULK_EXCEPTIONS(indx).ERROR_CODE);
END LOOP;
END;
Placing the keyword SAVE EXCEPTIONS in the FORALL loop stores all the encountered exceptions in BULK_EXCEPTIONS. Thus, the FORALL loop can be used for Bulk Insert and even Exceptions can be handled in that.

NOTE: The keyword SAVE EXCEPTIONS in the FORALL loop works only in Oracle9i.

Mahalakshmi Vijayaraj
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap