Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DBMS - Experiment 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Akhil Bharatiya Maratha Shikshan Parishad's

Anantrao Pawar College of Engineering & Research

Database Management

Experiment No:-

Title: Cursors: (All types: Implicit, Explicit, Cursor FOR Loop, Parameterized Cursor) Write a PL/SQL
block of code using parameterized Cursor, that will merge the data available in the newly created table
N_RollCall with the data available in the table O_RollCall. If the data in the first table already exist in the
second table then that data should be skipped.

Software used: Windows 11 Operating System, Oracle 11g, SQL developer

Theory:

Oracle creates a memory area, known as the context area, for processing an SQL statement,
which contains all the information needed for processing the statement; for example, the number of
rows processed, etc.

A cursor is a pointer to this context area. PL/SQL controls the context area through a cursor. A cursor
holds the rows (one or more) returned by a SQL statement. The set of rows the cursor holds is
referred to as the active set.

You can name a cursor so that it could be referred to in a program to fetch and process the rows
returned by the SQL statement, one at a time.

There are two types of cursors –

 Implicit cursors
 Explicit cursors

Implicit Cursors:

Implicit cursors are automatically created by Oracle whenever an SQL statement is executed, when
there is no explicit cursor for the statement. Programmers cannot control the implicit cursors and the
information in it.

Whenever a DML statement (INSERT, UPDATE and DELETE) is issued, an implicit cursor is
associated with this statement. For INSERT operations, the cursor holds the data that needs to be
inserted. For UPDATE and DELETE operations, the cursor identifies the rows that would be affected.
Akhil Bharatiya Maratha Shikshan Parishad's
Anantrao Pawar College of Engineering & Research

Database Management
In PL/SQL, you can refer to the most recent implicit cursor as the SQL cursor, which always has attributes
such as %FOUND, %ISOPEN, %NOTFOUND, and %ROWCOUNT. The SQL cursor has additional
attributes, %BULK_ROWCOUNT and %BULK_EXCEPTIONS, designed for use with the FOR ALL
statement. The following table provides the description of the most used attributes –

S.No Attribute & Description

%FOUND
1
Returns TRUE if an INSERT, UPDATE, or DELETE statement affected one or
more rows or a SELECT INTO statement returned one or more rows. Otherwise, it
returns FALSE.
%NOTFOUND
2
The logical opposite of %FOUND. It returns TRUE if an INSERT, UPDATE, or
DELETE statement affected no rows, or a SELECT INTO statement returned no
rows. Otherwise, it returns FALSE.
%ISOPEN
3
Always returns FALSE for implicit cursors, because Oracle closes the SQL cursor
automatically after executing its associated SQL statement

%ROWCOUNT
4
Returns the number of rows affected by an INSERT, UPDATE, or DELETE
statement, or returned by a SELECT INTO statement.

Explicit Cursors:

Explicit cursors are programmer-defined cursors for gaining more control over the context area. An
explicit cursor should be defined in the declaration section of the PL/SQL Block. It is created on a SELECT
Statement which returns more than one row.

The syntax for creating an explicit cursor is –

CURSOR cursor_name IS select_statement;


Akhil Bharatiya Maratha Shikshan Parishad's
Anantrao Pawar College of Engineering & Research

Database Management
Working with an explicit cursor includes the following steps–
Declaring the cursor for initializing the memory

 Opening the cursor for allocating the memory


 Fetching the cursor for retrieving the data
 Closing the cursor to release the allocated memory

Declaring the Cursor:

Declaring the cursor defines the cursor with a name and the associated SELECT statement. For
example –

CURSOR c_customers IS

SELECT id, name, address FROM customers;

Opening the Cursor:

Opening the cursor allocates the memory for the cursor and makes it ready for fetching the rows
returned by the SQL statement into it. For example, we will open the above defined cursor as follows –

OPEN c_customers;

Fetching the Cursor:

Fetching the cursor involves accessing one row at a time. For example, we will fetch rows from the
above-opened cursor as follows –

FETCH c_customers INTO c_id, c_name, c_addr;


Akhil Bharatiya Maratha Shikshan Parishad's
Anantrao Pawar College of Engineering & Research

Database Management
Closing the Cursor:

Closing the cursor means releasing the allocated memory. For example, we will close the above-
opened cursor as follows −

CLOSE c_customers;

Program Code:
mysql> use university
Database changed

mysql> select *from student;

+ + + + +
| sid | name | tot_cred | dep_name |
+ + + + +
| 1 | Aashish | 8 | Music |
| 2 | Rakesh | 8 | Music |
| 3 | Chetan | 8 | Music |
| 4 | Mayuri | 8 | Music |
| 5 | Priya |8 | Music |
| 6 | Tanvi |8 | Music |
| 7 | Anushka | 8 | Music |
+ + + + +

7 rows in set (0.00 sec)

mysql> delimiter //
mysql> create procedure cursor34()
-> begin
-> declare i int;
-> declare n varchar(45);
-> declare cur cursor for select tot_cred,name from student;
-> open cur;
-> fetch cur into i,n;
-> select i,n;
-> close cur;
-> end; //
Query OK, 0 rows affected (0.01 sec)
Akhil Bharatiya Maratha Shikshan Parishad's
Anantrao Pawar College of Engineering & Research

Database Management
Program Output:

mysql> delimiter ;
mysql> call cursor34();

+ + +
| i | n |
+ + +
| 8 | Aashish |
+ + +

1 row in set (0.00 sec)


Query OK, 0 rows affected (0.02 sec)

mysql> select tot_cred,name from student;


+ + +
| tot_cred | name |
+ + +
| 8 | Aashish |
| 9 | Rakesh |
| 9 | Chetan |
| 9 | Mayuri |
| 7 | Priya |
| 6 | Tanvi |
| 9 | Anushka |
+ + +
7 rows in set (0.00 sec)
Akhil Bharatiya Maratha Shikshan Parishad's
Anantrao Pawar College of Engineering & Research

Database Management

Conclusion:

You might also like