26

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

Embed Size (px)

Citation preview

Page 1: 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
Page 2: 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

Definition

Cursor is a control structure for the successive traversal of records in a result set. Cursor is a record pointer in a database.

DATABASECURSOR

Page 3: 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

Page 4: 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

DeclareOpenFetchCloseFree

Page 5: 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

This instruction associates a database cursor with an SQL statement in the current connection.

Page 6: 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

Forward only cursors (non-scrollable )Scrollable cursorsHold cursors

Page 7: 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

Sequential cursorForward 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.

Page 8: 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

Syntax

Example

DECLARE cursor_name CURSOR FOR SELECT ... FROM ...

DECLARE cursor1 CURSOR FOR SELECT * FROM customer_Table

Page 9: 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

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.

Page 10: 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

Syntax

Example

DECLARE cursor_name SCROLL CURSOR FOR SELECT ... FROM ...

DECLARE cursor1 SCROLL CURSOR FOR SELECT * FROM customer

Page 11: 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

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.

Page 12: 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

Syntax

Example

DECLARE cursor_name CURSOR WITH HOLD FOR SELECT ... FROM ...

DECLARE cursor1 CURSOR WITH HOLD FOR SELECT * FROM customer

Page 13: 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

Executes the SQL statement associated with a database cursor declared in the same connection.

Page 14: 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

OPEN cursor_name

Page 15: 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

Moves a cursor to a new row in the corresponding result set and retrieves the row values into fetch buffers.

Page 16: 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

Forward only cursor

Scrollable cursor

FETCH cursor_name INTO variable [,variable2…]

FETCH [ Direction ] cursor_name INTO variable [,variable2…]

Page 17: 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

NEXT -> retrieves the next row in the result setPREVIOUS,PRIOR

-> retrieves the previous row in the result setCURRENT

-> retrieves the current row in the result setFIRST

-> 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 setRELATIVE offset

-> moves offset rows in the result set and returns the row at the current position

Page 18: 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

Closes a database cursor and frees resources allocated on the database server for the result set.

Page 19: 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

CLOSE cursor_name

Page 20: 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

This instruction releases resources allocated to the database cursor with the DECLARE instruction.

Page 21: 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

FREE cursor_name

Page 22: 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

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 returnCURSOR_NAME%NOTFOUND

-> Return FALSE if the last FETCH returned a row

-> Return TRUE if the last FETCH did not returnCURSOR_NAME%ISOPEN

-> Return TRUE if the cursor is open

-> Return FALSE if the cursor is close

Page 23: 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

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

Page 24: 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

Syntax

UPDATE table_name SET ... WHERE CURRENT OF cursor_name

DELETE table_name SET ... WHERE CURRENT OF cursor_name

Page 25: 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

Thank you

Page 26: 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

Puris Chalermpug 5088055Anawat Boochabuppajarn 5088009Manthana  Atipremanon 5088016Thunyaluk Kumnurdchati 5088129