The Get Cursor command creates a cursor with the name of the specified Identifier by issuing a java.sql.Statement.executeQuery request on the value of the specified String.
If the String is a statement that does not generate a result set, the behavior of the underlying database determines whether an empty result set or an error is issued. If there is an error in executing the statement, no cursor is created.
ij sets the cursor name using a java.sql.Statement.setCursorName request. Behavior with respect to duplicate cursor names is controlled by the underlying database. Derby does not allow multiple open cursors with the same name.
Once a cursor has been created, you can use ij's Next and Close commands to step through its rows and (if the connection supports positioned update and delete commands) to alter the rows.
GET [ WITH { HOLD | NOHOLD } ] CURSOR Identifier AS String
WITH HOLD is the default attribute of the cursor. For a non-holdable cursor, use the WITH NOHOLD option.
ij> -- autocommit needs to be off so that the positioned update ij> -- can see the cursor it operates against. ij> autocommit off; ij> get cursor menuCursor as 'SELECT * FROM menu FOR UPDATE OF price'; ij> next menuCursor; COURSE |ITEM |PRICE ----------------------------------------------- entree |lamb chop |14 ij> next menuCursor; COURSE |ITEM |PRICE ----------------------------------------------- dessert |creme brulee |6 ij> UPDATE menu SET price=price+1 WHERE CURRENT OF menuCursor; 1 row inserted/updated/deleted ij> next menuCursor; COURSE |ITEM |PRICE ----------------------------------------------- appetizer |baby greens salad |7 ij> close menuCursor; ij> commit; ij>
ij> connect 'jdbc:derby:memory:dummy;create=true;user=john' as john_conn; ij> create table john_tbl(c int); 0 rows inserted/updated/deleted ij> insert into john_tbl values(1),(2),(3); 3 rows inserted/updated/deleted ij> connect 'jdbc:derby:memory:dummy;user=fred' as fred_conn; ij(FRED_CONN)> get cursor john_cursor@john_conn as 'select * from john_tbl'; ij(FRED_CONN)> next john_cursor@john_conn; C ----------- 1 ij(FRED_CONN)> next john_cursor@john_conn; C ----------- 2 ij(FRED_CONN)> next john_cursor@john_conn; C ----------- 3 ij(FRED_CONN)> next john_cursor@john_conn; No current row ij(FRED_CONN)> close john_cursor@john_conn; ij(FRED_CONN)> disconnect all; ij>