A Cursor is a pointer to this context area. Oracle creates context area for processing an SQL statement which contains all information about the statement. Show PL/SQL allows the programmer to control the context area through the cursor. A cursor holds the rows returned by the SQL statement. The set of rows the cursor holds is referred as active set. These cursors can also be named so that they can be referred from another place of the code. In this tutorial you will learn-
The cursor is of two types.
Implicit CursorWhenever any DML operations occur in the database, an implicit cursor is created that holds the rows affected, in that particular operation. These cursors cannot be named and, hence they cannot be controlled or referred from another place of the code. We can refer only to the most recent cursor through the cursor attributes. Explicit CursorProgrammers are allowed to create named context area to execute their DML operations to get more control over it. The explicit cursor should be defined in the declaration section of the PL/SQL block, and it is created for the ‘SELECT’ statement that needs to be used in the code. Below are steps that involved in working with explicit cursors.
Syntax:DECLARE CURSOR <cursor_name> IS <SELECT statement^> <cursor_variable declaration> BEGIN OPEN <cursor_name>; FETCH <cursor_name> INTO <cursor_variable>; . . CLOSE <cursor_name>; END;
Cursor AttributesBoth Implicit cursor and the explicit cursor has certain attributes that can be accessed. These attributes give more information about the cursor operations. Below are the different cursor attributes and their usage.
Explicit Cursor Example: We will project all the employee’s name from emp table using a cursor. We will also use cursor attribute to set the loop to fetch all the record from the cursor. Output Employee Fetched:BBB Employee Fetched:XXX Employee Fetched:YYY Total rows fetched is 3Code Explanation:
FOR Loop Cursor statement“FOR LOOP” statement can be used for working with cursors. We can give the cursor name instead of range limit in the FOR loop statement so that the loop will work from the first record of the cursor to the last record of the cursor. The cursor variable, opening of cursor, fetching and closing of the cursor will be done implicitly by the FOR loop. Syntax:DECLARE CURSOR <cursor_name> IS <SELECT statement>; BEGIN FOR I IN <cursor_name> LOOP . . END LOOP; END;
Oracle Cursor for Loop Example: Output Employee Fetched:BBB Employee Fetched:XXX Employee Fetched:YYYCode Explanation:
Note: In Cursor-FOR loop, cursor attributes cannot be used since opening, fetching and closing of the cursor is done implicitly by FOR loop.
In this tutorial we will learn Cursor in PL/SQL which is nothing but a pointer to the work area or context area of oracle engine. A cursor is a pointer to the work area or context area, used by the oracle engine for executing SQL statements. Such a work area is privately used for SQL operations by the oracle engine. When the oracle engine executes an SQL statements, the row of data returned is stored in cursor and is called active data set. And the cursor occupies memory size required to hold the number of rows in the active dataset. Cursor containing the values retrieved from a table are opened in the predefined area of the main memory by oracle engine. This data is then transferred to the client machine via network. Types of Cursor in PL/SQLCursor can be divided into two types based on the condition under which they are created and used:
Implicit CursorThe cursor which is automatically created, maintained and closed by the Oracle engine while execution of any DML(Data Manipulation Language) queries like INSERT, UPDATE or DELETE are called Implicit Cursor. Implicit Cursors are controlled by Oracle and programmers cannot access its information. When a DML statement is executed an implicit cursor is created and attached to it. Explicit CursorThe cursor which has to be created, maintained and closed by a program through PL/SQL code for the execution of any SELECT query that returns more than one row is called Explicit Cursor. It is a user-defined cursor declared in the Declare section of PL/SQL block and is used in its Executable section. Using Explicit CursorTo define a cursor, one need to follow the following steps:
To work with the cursor whether Implicit or Explicit cursor, there are following attributes which are used:
Time for an Example!Finally, let's see a cursor in action. Below we have a student table with 4 columns namely, ROLLNO, SNAME, AGE, COURSE.
Above table student will be used in following program, where we will use the SELECT query to fetch the names of all the students, store them in a cursor and then loop around the cursor to print the names. DECLARE CURSOR student_cursor IS SELECT sname FROM Student ; snm Student.sname %type; BEGIN OPEN student_cursor; IF student_cursor%ISOPEN FALSE then dbms_output.put_line('Cannot open cursor'); ELSE LOOP FETCH student_cursor INTO snm; IF student_cursor%NOTFOUND then Exit; END IF; dbms_ output.put_line('' ||snm); END LOOP; dbms_output.put_line('Total Records: ' ||student_cursor%rowcount); CLOSE student_cursor; END;Anu Asha Arpit Chetan Nihal Total Record: 5 PL/SQL procedure successfully completed. In the above program,
Cursor FOR LOOPA Cursor FOR LOOP is a loop meant for the cursor which automatically checks for the row count and exits the loop when all the data stored in the cursor is iterated. A cursor FOR loop automatically does the following:
Examples showing use of CURSOR FOR LOOPBelow we have a simple PL/SQL code block showing the use of Cursor For Loop: DECLARE CURSOR student_cursor IS SELECT sname FROM Student; BEGIN FOR snm IN student_cursor LOOP dbms_output.put_line('' || snm); END LOOP; END;Anu Asha Arpit Chetan Nihal PL/SQL procedure successfully completed. In the above program,
What is Parameterized Cursor?A parameterized cursor is a cursor with arguments and it allows us to create dynamic SQL queries with conditions containing the variables. Here we have the syntax for declaration of Parameterized Cursor: CURSOR cursor_name (variable_name Datatype) IS <SELECT statement...>;After declaring a parameterized cursor, when we open it we have to provide the value to be used in the parameterized cursor, like this: OPEN cursor_name(value/variable/expression);Let's take an example to demonstrate the use of Parameterized Cursors: set serveroutput on; DECLARE CURSOR showRec(sno student.rollno%type) IS SELECT sname, course FROM student WHERE rollno=sno; a student.sname%type; b student.course%type; c student.rollno%type; BEGIN d := &rollno; OPEN showRec(d); IF showRec%Isopen = FALSE then dbms_output.put_line('Cannot open Cursor'); ELSE LOOP FETCH showRec into a,b; EXIT WHEN showRec%NOTFOUND; dbms_output.put_line(a|| '' ||b); END LOOP; End IF; CLOSE showRec; END;Enter the value for d:12 -------------------- | SNAME | COURSE | ==================== | Asha | BCOM | -------------------- PL/SQL procedure successfully completed. In the above program,
|