0% found this document useful (0 votes)
0 views25 pages

lecture-6-1

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 25

DATABASE MANAGEMENT SYSTEM II

Lecture 6

Faculty of Computer and Information Technology Dr. ABDULRAZZAK ALI


Recap

•A Concept of Cursor.
•Implicit Cursor.
Today’s Lecture Outcomes

•Explicit cursor.
•Explicit Cursor Attributes.
•Cursor for loop.
•Parameterized Cursor.
Explicit Cursor

▪"A cursor is called an Explicit Cursor, if it is opened by user to


process data through PL/SQL block."
▪An explicit cursor is used when there is a need to process
more than one record individually.
▪In contrast to implicit cursors, explicit cursors are opened by
the user.
▪And so, user has to take care about managing such cursors.
Explicit Cursor
▪Explicit cursors are declared and mapped to SELECTstatement in the Declaration section of
the PL/SQL block.
▪ They are used within the Executable Commands section.
▪The steps required to manage explicit cursors and manipulate data are
given below:
1. Declare a Cursor 2. Open a Cursor 3. Fetching Data 4. Processing Data 5. Closing
Cursor
1. Declare Cursor

▪A cursor is defined in the Declaration section of the PL/SQL block


using following syntax.
▪Syntax:
CURSOR CursorName IS select…..;
▪A cursor with cursor Name is declared.
▪It is mapped to the query given by SELECTstatement.
1. Declare Cursor
▪Example:

CURSOR deposit_cur IS SELECTactno, bname, amount from


deposit;
▪This declaration only informs Oracle that a cursor with cursor name
will be used later.
▪No memory is allocated in this step.
2. Open Cursor
▪While opening cursor, following operations are performed. Theseoperations
initialize the given cursor.
1. Allocate memory required to store data.
2. Execute the SELECTstatement associated with cursor.
3. Create active data set by retrieving data from tables and
populating allocated memory with these data.
4. Set the cursor row pointer to point to the first record in active data set.
▪Syntax: OPEN cursorName;
▪Example: OPEN deposit_cur;
3. Fetch Data
▪To process a record individually, data stored in that record are
need to be fetched into memory variables.
▪This task is performed by the FETCHstatement.
▪Syntax:
FETCH CursorName INTO variable1, variable2,…. ;
Example:
FETCH deposit_cur INTO a_no, branch, bal;
3. Fetch Data
▪Retrieves data from the current row in the active data set
and stores them in givenvariables.
▪Data from single row (or record) are fetched at a time.
▪After fetching data, updates row pointer to point the next row in an
active data set.
▪Variables should be compatible with the columns specified in the
SELECTstatement.
4. Processing Data
▪This step involves actual processing of the table data.
▪Data are already fetched into variables.
▪These variables can be processed as per requirements.
▪This step may involve various PL/SQL as well as SQLstatements.
5. Close Cursor
▪A cursor should be closed after the processing of data completes.
▪Closing a cursor releases memory allocated for that cursor.
▪If user forgets to close the cursor, it will be automatically closed by
Oracle on termination of the program.
▪Syntax:
CLOSE cursorName ;
▪Example:
CLOSE deposit_cur ;
Explicit Cursor Attributes
ATTRIBUTES DESCRIPTION

%ISOPEN If cursor is open, returns TRUE:Else, returns FALSE.

SQL%FOUND If record was fetched successfully in last FETCH statement, returns


TRUE; Else, returns FALSE indicating no more records available in
active data set.
SQL%NOTFOUND If record was not fetched successfully in last FETCHstatement,
returns TRUE;Else, returns FALSE.

SQL%ROWCOUNT Returns the number of records fetched from the active data It is set
to zero when the cursor isopened.
Explicit Cursor (Example)

Give 8% interest whose balance is >=5000


and 4% whose balance is <5000
Cursor For Loop
▪A FETCH statement can fetch data from single row of an active data set.
▪But, there will be a need to process multiple rows most of the times.
▪So, FETCH statement is enclosed within a loop to process multiple rows.
This loop needs to be controlled by a programmer.
▪Oracle provides another loop-statement to control loops specifically for
cursors.
▪This statement is a variation of the basic FOR loop, and it is known
as cursor for loops.
Cursor For Loop
▪Syntax:
FOR LOOP variable IN CursorName
-- Execute commands...
END LOOP;
▪Example:
FOR LOOP dep_var IN deposit_cur
-- Execute commands...
END LOOP;
Cursor For Loop
▪This syntax performs the following operations, automatically:
▪A given variable is created of the %ROWTYPE. This variable refers to
the entire row of the active data set.
The individual fields of the records can be accessed using
syntax: variableName.columnName
▪Specified cursor is opened.
▪Data from the row of the active data set are fetched into
given variable for each iteration of the loop.
▪Exits from the loop and closes the cursor.
Cursor For Loop (Example)

Give 8% interest whose balance is >=5000


and 4% whose balance is <5000
Parameterized Cursor
▪Till now, the active data set is created as it contains all the records from the
given table.
▪There may be a need to create an active data set that contains only
selected records from the table.
▪For example, consider, there is a need to create an active data set that
contains only records which have balance greater than 7000.
▪For this purpose, it is necessary to provide a WHERE clause with SELECT
statement while declaring a cursor.
Parameterized Cursor
▪Oracle allows to pass parameters to cursors that can be used to provide
condition with WHERE clause.
▪If parameters are passed to cursor, that cursor is called a parameterized
cursor.
▪Syntax:
CURSOR cursor_name (variableName data Type) IS SELECT….;
▪ And, parameters can be passed to a cursor while opening it.
OPEN cursor_name ( value / variable / expression):
Parameterized Cursor (Example)

Give 10% interest whose balance is >=7000.


Example 1
Declare
Cursor Name_Emp Is select ename from emp;
N emp.ename%type;
Begin
Open Name_Emp;
Loop
Fetch Name_Emp into N;
If Name_Emp%notfound then
Exit;
End if;
DBMS_output.put_line(‘Employee Fetched :’ || N);
End loop;
DBMS_output.put_line(‘Total Rows Fetched :’ || Name_Emp%ROWCOUNT );
Close Name_Emp;
End;
Example 2

DECLARE
Cursor Name_Emp Is select ename from emp;
BEGIN
FOR N IN Name_Emp LOOP
Dbms_output.put_line(‘Employee Fetched:‘||N.emp_name);
END LOOP;
END;
Example 3
DECLARE
v_name VARCHAR2 (30);
--Declare
Cursor Emp_Cursor(var_e_id VARCHAR2) IS SELECT first_name FROM EMPLOYEES
WHERE employee_id < var_e_id;
BEGIN
OPEN Emp_Cursor(105);
LOOP FETCH Emp_Cursor INTO v_name;
EXIT WHEN Emp_Cursor %NOTFOUND;
DBMS_OUTPUT.PUT_LINE(v_name );
END LOOP;
CLOSE Emp_Cursor;
END;
Thank You…

You might also like