12.07.2015 Views

DB2 data manipulation

DB2 data manipulation

DB2 data manipulation

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

developerWorks®ibm.com/developerWorksand fifth tutorials in this series (on ODBC/CLI and the Java platform, respectively,refer to the Resources section).Cursor scopeWhen referring to the cursor scope, it means the period during which it is availablefor fetching rows. That period begins when the cursor's OPEN statement hascompleted successfully. By default, the scope of the cursor ends when the cursor isclosed or when a commit is executed. As you'll see on the next panel, having acommit end the scope can be a nuisance, but there's a way around it.WITH HOLD cursorsUsually, applications should be written to have COMMIT statements executed fairlyfrequently. These commit cause locks to be released, and they minimizeconcurrency issues between applications. However, committing can cause aproblem when it comes to cursors. This is where the WITH HOLD clause comes in.As an example, consider an application in which an SQL statement reads 10,000rows using a cursor. The application checks each row's contents and updates therow to set the value of a status column. Waiting to commit until all 10,000 rows havebeen processed could cause lock conflicts, so it commits after every 20 rows. Bydefault, however, the commit closes the cursor, so the position in the result setwould be lost and the application would have to do some special processing tocontinue properly from where it left off.The solution to this problem is to change the cursor definition to include the WITHHOLD clause. This causes a commit to leave the cursor open and avoid releasinglocks necessary to maintain the cursor's positioning. In other words, the WITH HOLDclause extends the scope of a cursor beyond a commit. Here's an example of aWITH HOLD cursor:DECLARE C1 CURSOR WITH HOLD FOR SELECT * FROM STAFFThe WITH HOLD clause has no effect on what happens to a cursor during a rollback.The cursor is closed and all associated locks are released.Section 6. Manipulate cursorsOverview of cursor processing<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 14 of 25© Copyright IBM Corporation 1994, 2007. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!