Professional Documents
Culture Documents
9th Dec
9th Dec
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;
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;
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;
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.
syntax:
ex:
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;
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;