Programs On Cursors
Programs On Cursors
INPUT:
declare
cursor c1 is select * from dept;
cursor c2 is select * from emp;
s emp.sal%type;
begin
for i in c1
loop
s:=0;
dbms_output.put_line('----------------------------------------------');
dbms_output.put_line('Department is :' || i.deptno ||' Department name is:' ||
i.dname);
dbms_output.put_line('-------------------------------------------');
for j in c2
loop
if ( i.deptno=j.deptno) then
s:=s+j.sal;
dbms_output.put_line(j.empno|| ''|| j.ename || ''|| j.sal );
end if;
end loop;
dbms_output.put_line('----------------------------------------------');
dbms_output.put_line('Total salary is: '|| s);
dbms_output.put_line('----------------------------------------------');
end loop;
end;
PL/SQL Block Using CURSOR TO Find EMPLOYEE WITH GIVEN JOB AND
DEPTNO
declare
cursor c1(j varchar2,dn number) is select empno,ename from emp where job=j and
deptno=dn;
row1 emp%rowtype;
jb emp.job%type;
d emp.deptno%type;
begin
jb:='&jb';
d:=&d;
open c1(jb,d);
fetch c1 into row1.empno,row1.ename;
if c1%notfound then
dbms_output.put_line('Employee does not exist');
else
dbms_output.put_line('empno is:'||row1.empno||'' ||'employee name
is:'||row1.ename);
end if;
end;
Write a Cursor to display the list of employees who are working as a
Managers or Analyst
INPUT:
declare
cursor c(jb varchar2) is select ename from emp where job=jb;
em emp.job%type;
begin
open c('MANAGER');
dbms_output.put_line(' EMPLOYEES WORKING AS MANAGERS ARE:');
loop
fetch c into em;
exit when c%notfound;
dbms_output.put_line(em);
end loop;
close c;
open c('ANALYST');
dbms_output.put_line(' EMPLOYEES WORKING AS ANALYST ARE:');
loop
fetch c into em;
exit when c%notfound;
dbms_output.put_line(em);
end loop;
close c;
end;
write a Cursor to display List of Employees from Emp Table in PL/SQL block
declare
cursor c is select empno,ename,deptno,sal from emp ;
i emp.empno%type;
j emp.ename%type;
k emp.deptno%type;
l emp.sal%type;
begin
open c;
dbms_output.put_line('Empno,name,deptno,salary of employees are:= ');
loop
fetch c into i,j,k,l;
exit when c%notfound;
dbms_output.put_line(i||''||j||''||k||''||l);
end loop;
close c;
end;
1. Write a PL/SQL program to list employee names whose slary is more than their
Manager ( to whom the/report) sal
declare
cursor c1 is
select e1.ename,e1.sal,e2.ename,e2.sal from emp e1,
emp e2 where e1.mgr=e2.empno and e1.sal>e2.sal;
e1 emp.ename%type;
e2 emp.sal%type;
e3 emp.ename%type;
e4 emp.sal%type;
begin
open c1;
loop
fetch c1 into e1,e2,e3,e4;
exit when c1%notfound;
dbms_output.put_line(e1||'---'||e2||'---'||e3||'---'||e4);
end loop;
close c1;
end;
2. Write a PL/SQL program to display TOP 3 earners of the company (list names &
Salary).
declare
ena emp.ename%type;
sa emp.sal%type;
cursor c1 is
select ename,sal from emp order by sal desc;
begin
open c1;
loop
fetch c1 into ena,sa;
exit when c1%rowcount>3;
dbms_output.put_line(ena||'............'||sa);
end loop;
close c1;
end;
begin
dbms_output.put_line('emp names and manager chain');
dbms_output.put_line('king');
for m in c1 loop
eno:=m.empno;
mno:=m.mgr;
while mno is not null
loop
select ename into ena from emp where empno=eno;
dbms_output.put(ena);
select mgr into mno from emp where empno=eno;
eno:=mno;
end loop;
dbms_output.put_line(' ');
end loop;
end;