0% found this document useful (0 votes)
37 views2 pages

Exp. No. 4

DBMS Exp. No 4

Uploaded by

9047007778
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views2 pages

Exp. No. 4

DBMS Exp. No 4

Uploaded by

9047007778
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Ex.

No: 4 Database Programming: Implicit and Explicit Cursors


Date:

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.

SQL>insert into customers values(&id, '&name', &age, '&address', &salary);

SQL> select * from customers;

ID Name Age Address Salary


1 Ramesh 32 Kovur 2500
2 Suresh 33 Avadi 3500
3 Dinesh 35 Chennai 3200
4 Kamlesh 23 Adayar 5000
5 tanessh 31 saidapet 2312

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 /

SQL> select * from customers;

ID Name Age Address Salary


1 Ramesh 32 Ahmedabad 2000
2 Khilan 33 Delhi 1500
3 Kaushik 35 Kota 2000
4 Chaitali 23 Mumbai 6500
5 Hardik 31 Bhopal 8500
6 Komal 22 MP 4500

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:

Following is a complete example to illustrate the concepts of explicit cursors

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

PL/SQL procedure successfully completed.

Result:

Thus the database have been created and executed successfully by using Implicit and
Explicit Cursors.

You might also like