Cursor Handling: Sbop Oracle Training at Tiet
Cursor Handling: Sbop Oracle Training at Tiet
SELECT empno, ename, job, sal FROM emp WHERE deptno = 10;
All the rows returned by the query are stored in the cursor at the Server
and will be as displayed at the client end.
Whenever any cursor is opened and used, the oracle engine creates a set
of four system variables, which keeps tack of the current status of a
cursor. These cursor variables can be accessed and used in a PL/SQL
block. Both Implicit and Explicit cursor have four attributes. They are
described as:
Implicit Cursors named “SQL” are declared by PL/SQL implicitly for all
SQL statements. Implicit cursors attributes can be used to access
information about the status of last insert, update, delete or single-row
select statements. This can be done by preceding the implicit cursor
attribute with the cursor name (i.e. SQL). The value of the cursor
attributes always refers to the most recently executed SQL statement,
wherever the statement appears.
BEGIN
DELETE EMP WHERE EMPNO=&EMPNO;
IF SQL%FOUND THEN
DBMS_OUTPUT.PUT_LINE(‘RECORD DELETED’);
ELSE
DBMS_OUTPUT.PUT_LINE(‘RECORD IS NOT DELETED’);
END IF;
END;
DECLARE
N NUMBER;
BEGIN
DELETE EMP WHERE DEPTNO=&DEPTNO;
N:=SQL%ROWCOUNT;
DBMS_OUTPUT.PUT_LINE(‘TOTAL NUMBER OF RECORD
DELETED’ || N);
END;
SBoP Oracle Training at TIET
To display the total number of rows affected by an
UPDATE DML on employees of particular department (input
by user) by certain percentage entered by user. If
entered department does exist then to display the message
‘DEPARTMENT DOES NOT EXIST’.
Syntax:
Close <cursor-name>;
DECLARE
CURSOR C1 IS SELECT EMPNO, ENAME, JOB FROM EMP
WHERE
DEPTNO=10;
REC C1%ROWTYPE; /*rec is a row type variable for cursor c1 record,
containing empno, ename and job*/
BEGIN
OPEN C1;
LOOP
Store the employee number, old salary and new salary in a table temp
having three columns empno, old and new.
END;
Example 17.7: Consider a PL/SQL code to calculate the total and
percentage of marks of the students in four subjects from table result,
which has the following structure:
(rno,s1,s2,s3,s4,total,percentage).
The rollno and marks in each subject are stored in the database, PL/SQL
code calculate the total and percentage of each student and update the
database.
It is a machine defined loop exit i.e. when all the values in the FOR
construct are exhausted looping stops.
SBoP Oracle Training at TIET
A cursor for loop automatically does the following:
♦ Opens a cursor.
Solution:
Solution:
Solution: