Quote:
|
Originally Posted by cprash.aggarwal
Actually the SP was just an example for the scenario i mentioned,
Actual SP is complex to write through a single statement. My question if we get large number of records in select stmnt and we have to process them one by one, how can we tackle this optimally, shall we fetch all records in one go in the cursor, say i have 1 million records, cant we process them in batches but all should be processed in one sp
|
You can process them in one cursorin a SP, but do the following:
1. Declare the cursor using "WITH HOLD" and "WITH UR". This make sure that the cursor is not closed when do a commit and that it will minimize lock contention problems. Do not put an order by in the cursor.
2. After the fetch, use a separate update statment to update the rows you want to change using the primary key. Every 100 -1000 updates, do a COMMIT. This will reduce lock contention and make sure the transaciton logs will not fill up. Make sure you do an extra COMMIT at the end of program.
This technique can be used on tables with billions of rows.