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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Application Security Testing: An Integral Part of DevOps


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
Thanks for your registration, follow us on our social networks to keep up-to-date