Exp. No. 4
Exp. No. 4
AIM:
To implement implicit and explicit cursors using PL/SQL programs.
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.
Example:
SQL> create table customers(id varchar(10), name varchar(10), age varchar(10), address
varchar(10), salary varchar(10));
Table Created.
The following program will update the table and increase the salary of each customers by 500 and
use the SQL%ROWCOUNT attribute to determine the number of rows affected-
PL/SQL PROGRAM :
SQL> DECLARE
2 total_rows number(2);
3 BEGIN
4 UPDATE customers
5 SET salary = salary +500;
6 IF sql%notfound THEN
7 dbms_output.put_line('no customers selected');
8 ELSIF sql%found THEN
9 total_rows :=sql%rowcount;
10 dbms_output.put_line(total_rows || 'customers selected');
11 END IF;
12 END;
13 /
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.
Example:
SQL>DECLARE
2 c_id customers.id%type;
3 c_name customers.name%type;
4 c_addr customers.address%type;
5 CURSOR c_customers is
6 SELECT id, name, address FROM customers;
7 BEGIN
8 OPEN c_customers;
9 LOOP
10 FETCH c_customers into c_id, c_name, c_addr;
11 EXIT WHEN C_customers%notfound;
12 dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);
13 END LOOP;
14 CLOSE c_customers;
15 END;
16 /
When the above code is executed at the SQL prompt, it produces the following result-
1 Ramesh Ahmedabad
2 Khilan Delhi
3 kaushik Kota
4 Chaitali Mumbai
5 Hardik Bhopal
6 Komal MP
Result:
Thus the database have been created and executed successfully by using Implicit and
Explicit Cursors.