Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

-- Program for to display top 5 employee salary details.

Declare
Cursor Emp_cur is Select empno,ename,job,hiredate, sal,deptno from emp order by
sal desc;
e emp_cur%rowtype;
begin
open Emp_cur;
Loop
Fetch emp_cur into e;
Exit when Emp_cur%rowcount>5;
Dbms_output.put_line(e.empno||' '||e.ename||' '||e.sal||' '||e.hiredate||' '||
e.deptno);
End Loop;
Close Emp_cur;
End;

-- program for to display alternative records.

Declare
Cursor Emp_cur is select * from emp;
e emp_cur%rowtype;
Begin
Open Emp_cur;
Loop
Fetch Emp_cur into e;
Exit when Emp_cur%notfound;
if Mod(Emp_cur%rowcount,2)=0 then
dbms_output.put_line(emp_cur%rowcount||' '||e.empno||' '||e.ename||' '||
e.sal||
' '||e.hiredate||' '||e.deptno);
end if;
End Loop;
Close Emp_cur;
End;

-- Cursors using While Loop

Declare
Cursor Emp_cur is select * from emp;
E emp_cur%rowtype;
Begin
Open Emp_cur;
Fetch Emp_cur into e;
While(Emp_cur%found)
Loop
dbms_output.put_line(e.empno||' '||e.ename||' '||e.sal||' '||e.deptno);
Fetch Emp_cur into e;
End Loop;
Close Emp_cur;
End;

-- cursors with parameters:


------------------------------------
-> While opening the cursor to provide parameter value is called as cursor with
parameters.
-> max. 32 parameters are supported.
Declare
Cursor Emp_cur(Pdeptno Number) is select * from emp
where Deptno=Pdeptno;
e emp_cur%rowtype;
Begin
Open emp_cur(&deptno);
Loop
Fetch Emp_cur into e;
Exit when emp_cur%notfound;
dbms_output.put_line(e.empno||' '||e.ename||' '||e.job||' '||e.sal||' '||
e.deptno);
End Loop;
Close Emp_cur;
End;

ii. Implicit Cursors:


------------------------------------------

-> these cursors are created by Oracle implicitely.

Begin
Delete from Emp where Deptno=&deptno;
if Sql%rowcount>3 then
rollback;
else
commit;
dbms_output.put_line('Records Deleted Successfully.');
End if;
End;

Static Cursor:
------------------

Declare
Cursor Emp_cur is select * from emp;
e emp_cur%rowtype;
Begin
Open Emp_cur;
Loop
Fetch Emp_cur into e;
Exit when Emp_cur%notfound;
dbms_output.put_line(e.empno||' '||e.ename||' '||e.sal||' '||e.hiredate||' '||
e.deptno);
End Loop;
Close Emp_cur;
dbms_output.put_line('-----------------------------------');
Open Emp_cur;
Loop
Fetch Emp_cur into e;
Exit when Emp_cur%notfound;
dbms_output.put_line(e.empno||' '||e.ename||' '||e.sal||' '||e.hiredate||' '||
e.deptno);
End Loop;
Close Emp_cur;
End;

-- Dynamic Cursor
-> to declare the cursor without any select statment.

-> select statement will be provided while opening the cursor.

syntax:

type <cursor_name> is ref cursor;

ex:

type r_cur is ref cursor;

Declare
Type R_cur is Ref Cursor;
my_cur R_cur;
E emp%rowtype;
D dept%rowtype;
Begin
Open My_cur for Select * from Emp;
Loop
Fetch My_cur into E;
Exit when My_cur%notfound;
dbms_output.put_Line(E.Empno||' '||E.Ename||' '||E.Job||' '||E.Sal||' '
||E.Hiredate||' '||E.Deptno);
End Loop;
Close My_cur;
dbms_output.put_line('****************************************');
Open My_cur for Select * from Dept;
Loop
Fetch My_cur into D;
Exit when my_cur%notfound;
Dbms_output.put_line(D.Deptno||' '||D.Dname||' '||D.Loc);
End Loop;
Close My_cur;
End;

-- For Cursor

Declare
Cursor Emp_cur is select * from emp;
Begin
For i in Emp_cur
Loop
dbms_output.put_line(i.empno||' '||i.ename||' '||i.job||' '||i.hiredate||' '||
i.sal||' '
||i.deptno);
End Loop;
End;

-- cursors using aliases.

Declare
cursor my_cur is select Deptno, Max(sal) as Maxsal, Min(sal) as Minsal,
Sum(sal) as Sumsal, Avg(sal) as Avgsal from emp group
by deptno;
Begin
For i in my_cur
Loop
dbms_output.put_line(i.Deptno||' '||i.Maxsal||' '||i.Minsal||' '||i.Sumsal||'
'||Round(i.Avgsal,2));
End Loop;
End;

You might also like