Cursors 3
Cursors 3
Whenever DML statements are executed, a temporary work area is created in the system memory
and it is called a cursor. A cursor can have more than one row, but processing wise only 1 row is taken
into account. Cursors are very helpful in all kinds of databases like Oracle, SQL Server, MySQL, etc.
They can be used well with DML statements like Update, Insert and Delete. Especially Implicit cursors
are there with these operations. From time to time it changes the values and hence the implicit
cursor attribute values need to be assigned in a local variable for further use. In PL/SQL, two different
types of cursors are available.
• Implicit cursors
• Explicit cursors
Explicit cursors
Explicit cursors are defined by the programmers to have more control area on the context area. It has
to be defined in the declaration section of the PL/SQL Block. Usually, It is defined on a SELECT
Statement and it returns more than one row as output. We can iterate over the rows of data and
perform the required operations.
CURSOR <cursorName> IS
OPEN <cursorName>;
CLOSE <cursorName>;
Example:
DECLARE
empId employees.EMPLOYEEID%type;
empName employees.EMPLOYEENAME%type;
empCity employees.EMPLOYEECITY%type;
CURSOR c_employees is
BEGIN
OPEN c_employees;
LOOP
END LOOP;
CLOSE c_employees ;
END;
Output:
Implicit cursors
For DML statements, implicit cursors are available in PL/SQL i.e. no need to declare the cursor, and
even for the queries that return 1 row, implicit cursors are available. Through the cursor attributes,
we can track the information about the execution of an implicit cursor.
Implicit cursor attributes provide the results about the execution of INSERT, UPDATE, and DELETE.
We have different Cursor attributes like “%FOUND”, “%ISOPEN”, “%NOTFOUND”, and %ROWCOUNT.
The most recently executed SQL statement result will be available in Cursor. Initially cursor value will
be null.
Let us see the different cursor attributes one by one with regards to the DML statements. So let us
create a sample table named “employees” in oracle:
EMPLOYEECITY varchar2(50)
);
%FOUND attribute
DECLARE
employeeNo NUMBER(4) := 2;
BEGIN
END IF;
END;
Output:
%FOUND attribute and performing update operation example
DECLARE
employeeNo NUMBER(4) := 2;
BEGIN
END IF;
END;
%FOUND attribute upon update operation and then performing delete operation example
DECLARE
employeeNo NUMBER(4) := 2;
BEGIN
UPDATE tempory_employee2 SET employeeCity = 'Gurgaon' WHERE employeeId = employeeNo;
END IF;
END;
Output:
%ISOPEN Attribute: For Implicit Cursors, always the result is False. The reason is Oracle closes
immediately after executing the DML result. Hence the result is FALSE.
%NOTFOUND Attribute: It is just the opposite of %FOUND. %NOTFOUND is the logical opposite of
%FOUND. %NOTFOUND results in TRUE value for an INSERT, UPDATE, or DELETE statement which
affected no rows. By default, it returns False.
%ROWCOUNT Attribute: A number of rows affected by an INSERT, UPDATE or DELETE statement are
given by %ROWCOUNT. When there are no rows are affected, %ROWCOUNT gives 0 as the result,
otherwise, it returns the number of rows that have been deleted.
BEGIN
END;
Output:
The values of the cursor attribute have to be saved in a local variable and those variables can be used
in future uses. The reason is while doing multiple database operations in different blocks, cursor
attribute values keep on changing and hence this is much required.
The %NOTFOUND attribute is better used only with DML statements but not with SELECT INTO
statement.