0% found this document useful (0 votes)
17 views

DB2 Unit 11

The document discusses cursor processing in DB2. A cursor is a pointer that identifies the current row in a result table and allows a program to iterate through multiple returned rows like a file. There are four main steps to using a cursor: 1) declare the cursor to define the result table, 2) open the cursor to generate the result table, 3) fetch rows from the result table one at a time, and 4) close the cursor to release resources. Cursors can also be used to update or delete rows in the result table.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

DB2 Unit 11

The document discusses cursor processing in DB2. A cursor is a pointer that identifies the current row in a result table and allows a program to iterate through multiple returned rows like a file. There are four main steps to using a cursor: 1) declare the cursor to define the result table, 2) open the cursor to generate the result table, 3) fetch rows from the result table one at a time, and 4) close the cursor to release resources. Cursors can also be used to update or delete rows in the result table.
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 9

DB2 TRANSPARENCIES (Unit 11)

DB2_Tr Ver. 1.0.0 04/12/1998

Page 1 of 9

18 Cursor Processing
Objectives - Student will
Understand how a cursor is used Be able to process multiple rows using cursor verbs

DB2_Tr Ver. 1.0.0 04/12/1998

Page 2 of 9

18 Cursor Processing

Is a pointer that identifies the current row in a results table Used when multiple rows might be returned to the result table Can work through a result table much like reading through a standard sequential file A program may have more than one cursor

DB2_Tr Ver. 1.0.0 04/12/1998

Page 3 of 9

18 Cursor Processing
Step 1 - DECLARE CURSOR
Defines the result table to DB2 but does not create the result table

EXEC SQL DECLARE cursor_name CURSOR FOR

SELECT column1, column2, column3 FROM table-name

WHERE predicate END-EXEC.


DB2_Tr Ver. 1.0.0 04/12/1998 Page 4 of 9

18 Cursor Processing
Step 2 - OPEN cursor_name
Generates the results table specified by the DECLARE

EXEC SQL OPEN cursor_name END-EXEC.

DB2_Tr Ver. 1.0.0 04/12/1998

Page 5 of 9

18 Cursor Processing
Step 3 - FETCH
Advances the cursor one row in the results table

EXEC SQL FETCH cursor_name INTO :host-variable1,:host-variable2,,,, END-EXEC. IF SQLCODE = +100 END-OF-CURSOR............
DB2_Tr Ver. 1.0.0 04/12/1998 Page 6 of 9

18 Cursor Processing
Step 4 - CLOSE
Releases the results table associated with the cursor Releases resources

EXEC SQL CLOSE cursor_name END-EXEC.

DB2_Tr Ver. 1.0.0 04/12/1998

Page 7 of 9

18 Cursor Processing
UPDATE Using a Cursor
EXEC SQL DECLARE cursor_name CURSOR SELECT FOR

column1, column2, column3

FROM table_name WHERE predicate

FOR UPDATE OF column1, column2, column3 END-EXEC. EXEC SQL UPDATE table_name SET column1 = :column1 WHERE CURRENT OF cursor_name DB2_Tr Ver. 1.0.0 04/12/1998 END-EXEC. Page 8 of 9

18 Cursor Processing
DELETE Using a CURSOR
EXEC SQL DECLARE cursor_name CURSOR SELECT FOR

column1, column2, column3

FROM table_name WHERE END-EXEC. EXEC SQL DELETE FROM table_name WHERE CURRENT OF cursor_name END-EXEC. predicate

DB2_Tr Ver. 1.0.0 04/12/1998

Page 9 of 9

You might also like