DB2 SQL CURSOR

DECLARE cursor-name CURSOR FOR select-statement
OPEN cursor-name
FETCH FROM cursor-name INTO :host-variable1,:host-variable2
CLOSE cursor-name

1. SERIAL CURSOR

DECLARE @Customer CURSOR FOR
(SELECT * FROM CUSTOMER Where CUS_COUNTRY = 'INDIA')
OPEN @Customer
FETCH @Customer 
into :CustomerId,:CustomerName,:CustomerCountry
Exec SQL FETCH @Customer 
into :CustomerId,:CustomerName,:CustomerCountry;
DoW SQLCODE = 0;
// some code to process date read from Cursor.
Exec SQL FETCH @Customer
into :CustomerId,:CustomerName,:CustomerCountry;
EndDo;
CLOSE @Customer

2. SCROLLABLE CURSOR

DECLARE @Customer SCROLL CURSOR FOR
(SELECT * FROM CUSTOMER Where CUS_COUNTRY = 'INDIA')
OPEN @Customer
FETCH [direction-option] From cursor-name into :host-var1,:host-var2
FETCH LAST From @Customer 
into :CustomerId,:CustomerName,:CustomerCountry
FETCH ABSOLUTE 2 From @Customer 
into :CustomerId,:CustomerName,:CustomerCountry
FETCH PRIOR From @Customer 
into :CustomerId,:CustomerName,:CustomerCountry
CLOSE @Customer

CURSOR and COMMITMENT CONTROL

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store