Cursor
Inorder to be able work on a subset of the rows in a query, Phoenix provides support for CURSOR control structure. The following sequence shows how to use cursor.
-
Define cursor for a query using DECLARE statement
PreparedStatement statement = conn.prepareStatement("DECLARE empCursor CURSOR FOR SELECT * FROM EMP_TABLE"); statement.execute();
-
Open the cursor
statement = con.prepareStatement("OPEN empCursor"); statement.execute();
-
Fetch subset of rows to work with
statement = con.prepareStatement("FETCH NEXT 10 ROWS FROM empCursor"); ResultSet rset = statement.execute();
-
Iterate through the subset of rows and process them as required
while (rset.next != null){ ... }
-
Fetch additional set of rows to process and finally close the cursor when done
statement = con.prepareStatement(“CLOSE empCursor"); statement.execute();