Experiment no5 dbms
Experiment no5 dbms
4 %ROWCOUNT
Returns the number of rows affected by an INSERT, UPDATE, or
DELETE statement, or returned by a SELECT INTO statement.
Any SQL cursor attribute will be accessed as SQL%attribute_name as shown
below in the example.
Example
Select * from customers;
The following program will update the table and increase the salary of each
customer by 500 and use the SQL%ROWCOUNT attribute to determine the
number of rows affected -
When the above code is executed at the SQL prompt, it produces the following
result -
6 customers selected
PL/SQL procedure successfully completed.
If you check the records in customers table, you will find that the rows have
been updated — Select * from customers;
NAME AGE ADDRESS SALARY
1 32 Ahmedabad 2500.00
2 25 Delhi 2000.00
Ramesh
3 23 Kota 2500.00
Khilan
4 25 Mumbai 7000.00
Kaushik
5 27 Bhopal 9000.00
Chaitali
6 Hardik Komal 22 5000.00
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;
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
Closing the Cursor
Closing the cursor means releasing the allocated memory. For example, we will
close the aboveopened cursor as follows -
CLOSE c customers,
Example
Following is a complete example to illustrate the concepts of explicit cursors
&minua;