Presentation on theme: "Definition Cursor is a control structure for the successive traversal of records in a result set. Cursor is a record pointer in a database. DATABASE CURSOR."— Presentation transcript:
Definition Cursor is a control structure for the successive traversal of records in a result set. Cursor is a record pointer in a database. DATABASE CURSOR
Use For A cursor is used for processing individual rows returned by the database system for a query. And use for handle a result set
Declare Open Fetch Close Free
This instruction associates a database cursor with an SQL statement in the current connection.
Forward only cursors (non-scrollable ) Scrollable cursors Hold cursors
Sequential cursor Forward only cursor can fetch only the next row in sequence from the result set. On each execution of the FETCH statement, the database server returns the contents of the current row and locates the next row in the result set.
Syntax Example DECLARE cursor_name CURSOR FOR SELECT... FROM... DECLARE cursor1 CURSOR FOR SELECT * FROM customer_Table
Scrollable cursor can fetch rows of the result set in any sequence. You can fetch the first, last, or any intermediate rows of the result set as well as fetch rows repeatedly without having to close and reopen the cursor.
Syntax Example DECLARE cursor_name SCROLL CURSOR FOR SELECT... FROM... DECLARE cursor1 SCROLL CURSOR FOR SELECT * FROM customer
Ordinarily, all cursors close at the end of a transaction (COMMIT or ROLLBACK). A hold cursor does not close. It remains open after a transaction ends. A hold cursor can be either a sequential cursor or a scrollable cursor.
Syntax Example DECLARE cursor_name CURSOR WITH HOLD FOR SELECT... FROM... DECLARE cursor1 CURSOR WITH HOLD FOR SELECT * FROM customer
Executes the SQL statement associated with a database cursor declared in the same connection.
Moves a cursor to a new row in the corresponding result set and retrieves the row values into fetch buffers.
Forward only cursor Scrollable cursor FETCH cursor_name INTO variable [,variable2…] FETCH [ Direction ] cursor_name INTO variable [,variable2…]
NEXT -> retrieves the next row in the result set PREVIOUS,PRIOR -> retrieves the previous row in the result set CURRENT -> retrieves the current row in the result set FIRST -> retrieves the first row in the result set. LAST -> retrieves the last row in the result set. ABSOLUTE position -> retrieves the row at position in the result set RELATIVE offset -> moves offset rows in the result set and returns the row at the current position
Closes a database cursor and frees resources allocated on the database server for the result set.
This instruction releases resources allocated to the database cursor with the DECLARE instruction.
CURSOR_NAME%ROWCOUNT -> Return the number of row fetched so far CURSOR_NAME%FOUND -> Return TRUE if the last FETCH returned a row -> Return FALSE if the last FETCH did not return CURSOR_NAME%NOTFOUND -> Return FALSE if the last FETCH returned a row -> Return TRUE if the last FETCH did not return CURSOR_NAME%ISOPEN -> Return TRUE if the cursor is open -> Return FALSE if the cursor is close
Cursors can not only be used to fetch data from the DBMS into an application but also to identify a row in a table to be updated or deleted
Syntax UPDATE table_name SET... WHERE CURRENT OF cursor_name DELETE table_name SET... WHERE CURRENT OF cursor_name