12.07.2015 Views

DB2 data manipulation

DB2 data manipulation

DB2 data manipulation

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

developerWorks®ibm.com/developerWorksA simple example: Cursor usage in static embedded SQLBefore cursors are discussed further, look at an example of a very simple cursor instatic embedded SQL (a C program).EXEC SQL DECLARE C0 CURSOR FOR SELECT DEPTNUMB, DEPTNAME FROM ORG;EXEC SQL OPEN C0;EXEC SQL FETCH C0 INTO :DEPTNUMB, :DEPTNAME;WHILE (SQLCA.SQLCODE != 100) /* CONTINUE UNTIL THE END OF THE RESULT SET */{PRINTF(" %8D %-14S\N", DEPTNUMB, DEPTNAME);EXEC SQL FETCH C0 INTO :DEPTNUMB, :DEPTNAME;}EXEC SQL CLOSE C0;This code prints the number and name of every department in the org table. TheDECLARE CURSOR statement provides the query to be used, and the OPENstatement prepares the result set for the query. A FETCH statement is usedrepeatedly to move the values of the result set columns into program variables, onerow at a time, until the end of the result set is reached (SQLCODE = +100), atwhich point the cursor is closed.Section 5. Identify types of cursorsCursor characteristicsThere are three primary characteristics of cursors:• The cursor type: Read-only, up<strong>data</strong>ble, or ambiguous• The cursor direction(s): Forward-only or scrollable• The cursor scopeThese characteristics are discussed in the next few sections.Cursor type<strong>DB2</strong> handles each of the three cursor types somewhat differently, with thedifferences being primarily in the realm of performance. Take a look at each of them.Read-only cursors<strong>DB2</strong> <strong>data</strong> <strong>manipulation</strong>Page 12 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!