Csci235 Spring 2010 Tutorials Lab 7 With Answers!!!
Csci235 Spring 2010 Tutorials Lab 7 With Answers!!!
Implicit Cursor: Oracle automatically declares an implicit cursor every time a SQL statement is
executed. The user is unaware of this and cannot control or process the information in an implicit
cursor. The implicit cursor is used to process INSERT, UPDATE, DELETE and SELECT statements.
During the processing of an implicit cursor, oracle automatically performs the OPEN, FETCH and
CLOSE operations.
Explicit Cursor: The program defines an explicit cursor for any query that returns more than one row
of data. This means that the programmer has declared the cursor within the PL/SQL code block. This
declaration allows the application to sequentially process each row of data as the cursor returns it.
The advantage of declaring an explicit cursor over and implicit cursor is that the explicit cursor gives
the programmer more programmatic control. Also, implicit cursors are less efficient than explicit
cursors, so it is harder to trap data errors.
2. Opening the cursor. The declared cursor is opened, and memory is allocated.
3. Fetching the cursor. The declared and opened cursor can now retrieve data.
4. Closing the cursor. The declared, opened , and fetched cursor must be closed to release the
memory allocation.
Example:
DECLARE
v_zip c_zip%rowtype;
BEGIN
OPEN c_zip;
LOOP
END LOOP;
END;
Cursor Attributes
Tasks
Assume we have a student table with columns id, firstname, last name, age, average and gender. Write
PL/SQLprograms for following scenarios:
1. Write a program to get all female student information and then shows their firstname,
lastname and age.
SQL> DECLARE
3 v_stu c_stu%rowtype;
4 BEGIN
5 OPEN c_stu;
6 LOOP
10 END LOOP;
11 CLOSE c_stu;
12 END;
13 /
tooba sheikh 20
hina majeed 19
aisha aleem 22
huma g 25
2. Write a program which shows the total number of students who are younger than 23.
SQL> DECLARE
3 v_stu c_stu%rowtype;
4 BEGIN
5 OPEN c_stu;
6 LOOP
9 DBMS_OUTPUT.PUT_LINE (c_stu%rowcount);
11 END LOOP;
12 CLOSE c_stu;
13 END;
14 /
tooba sheikh
hina majeed
3
aisha aleem
3. Write a program to show firstname and lastname of students who their average is above 80.
SQL> DECLARE
3 v_stu c_stu%rowtype;
4 BEGIN
5 OPEN c_stu;
6 LOOP
10 END LOOP;
11 CLOSE c_stu;
12 END;
13 /
tooba sheikh
hina majeed
ahmed k
rehan s
SQL> DECLARE
2 Cursor c_stu IS SELECT * FROM student;
3 total number:=0;
4 v_stu c_stu%rowtype;
5 BEGIN
6 OPEN c_stu;
7 LOOP
10 total:=total +v_stu.age ;
12 END LOOP;
13 DBMS_OUTPUT.PUT_LINE (total/c_stu%rowcount);
14 CLOSE c_stu;
15 END;
16 /
tooba sheikh
hina majeed
aisha aleem
huma g
ahmed k
rehan s
hamza b
24.42857142857142857142857142857142857143