Cursor in PL/SQLIn 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. Show
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. To define a cursor, one need to follow the following steps:
Cursor ArributesTo 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:
Syntax: FOR variable_name IN cursor_name LOOP -- Executable statements END LOOP;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,
What is the syntax of an implicit cursor attribute Isopen *?An implicit cursor has attributes that return information about the most recently run SELECT or DML statement that is not associated with a named cursor. Note: You can use cursor attributes only in procedural statements, not in SQL statements. SQL%ISOPEN always has the value FALSE .
Which of the following is correct syntax to implicit cursor attribute?Implicit Cursor Attributes. Which of the following are implicit cursor attributes Mcq?C. The latest implicit cursor is called the SQL cursor and has the attributes like %FOUND, %ISOPEN, %NOTFOUND, and %ROWCOUNT.
Can we use Isopen attribute with implicit cursor?Initially cursor value will be null. %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.
|