0
3.5kviews
Explain Cursors in DBMS.
1 Answer
0
71views

A cursor is a temporary work area created in the system memory when a SQL statement is executed. A cursor contains information on a select statement and the rows of data accessed by it. This temporary work area is used to store the data retrieved from the database, and manipulate this data.

A cursor can hold more than one row, but can process only one row at a time. The set of rows the cursor holds is called the active set.

There are two types of cursors in PL/SQL :

  1. Implicit cursors.
  2. Explicit cursors.

Both implicit and explicit cursors have the same functionality, but they differ in the way they are accessed.

Cursor Attributes

1. %FOUND: Returns TRUE if record was fetched successfully, FALSE otherwise.

2. %NOTFOUND: Returns TRUE if record was not fetched successfully, FALSE otherwise.

3. %ROWCOUNT: Returns number of records fetched from cursor at that point in time.

4. %ISOPEN: Returns TRUE if cursor is open, FALSE otherwise.

Implicit cursors:

These are created by default when DML statements like, INSERT, UPDATE, and DELETE statements are executed. They are also created when a SELECT statement that returns just one row is executed.

When you execute DML statements like DELETE, INSERT, UPDATE and SELECT statements, implicit statements are created to process these statements.

Oracle provides few attributes called as implicit cursor attributes to check the status of DML operations. The cursor attributes available are %FOUND, %NOTFOUND, %ROWCOUNT, and %ISOPEN.

For example, When you execute INSERT, UPDATE, or DELETE statements the cursor attributes tell us whether any rows are affected and how many have been affected.

When a SELECT... INTO statement is executed in a PL/SQL Block, implicit cursor attributes can be used to find out whether any row has been returned by the SELECT statement. PL/SQL returns an error when no data is selected.

Consider the PL/SQL Stock that uses implicit cursor attributes as shown below:

Example:

DECLARE

$\hspace{10mm}$ Eid number(3);

BEGIN

$\hspace{10mm}$UPDATE emp set eid=&eid where salary=&salary;

eid:=sql%rowcount;

$\hspace{10mm}$ IF SQL%found then

$\hspace{20mm}$ dbms_output.put_line('success');

$\hspace{10mm}$ ELSE

$\hspace{20mm}$dbms_output.put_line ( ' not' ) ;

$\hspace{10mm}$ END IF;

$\hspace{20mm}$ dbms_output.put_line( 'rowcount'||eid);

END;

/

Explicit Cursors:

They must be created when you are executing a SELECT statement that returns more than one row. Even though the cursor stores multiple records, only one record can be processed at a time, which is called as current row. When you fetch a row the current row position moves to next row.

An explicit cursor is defined in the declaration section of the PL/SQL Block. It is created on a SELECT Statement which returns more than one row. We can provide a suitable name for the cursor.

Syntax:

CURSOR cursor_name IS select_statement;

cursor _name -A suitable name for the cursor.

Select_statement - A select query which returns multiple rows.

Please log in to add an answer.

Continue reading...

The best way to discover useful content is by searching it.

Search