0% found this document useful (0 votes)
50 views

Programs On Cursors

This document contains 4 PL/SQL programs that use cursors to retrieve and display employee data from the EMP table in different ways: 1. Display employee names whose salary is more than their manager's salary. 2. Display the top 3 highest earning employees and their salaries. 3. Display each employee name and total salary (salary + commission) in words. 4. Display each employee name and the chain of manager names above them.

Uploaded by

priyanka tata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Programs On Cursors

This document contains 4 PL/SQL programs that use cursors to retrieve and display employee data from the EMP table in different ways: 1. Display employee names whose salary is more than their manager's salary. 2. Display the top 3 highest earning employees and their salaries. 3. Display each employee name and total salary (salary + commission) in words. 4. Display each employee name and the chain of manager names above them.

Uploaded by

priyanka tata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Program to display the list of Employees and Total Salary Department wise.

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;

3. List employee names and their total salary (sal+comm.) in words.


declare
cursor c is
select ename,sal,comm from emp;
en emp.ename%type;
s1 emp.sal%type;
c1 emp.comm%type;
word1 words.word%type;
i number;
begin
open c;
loop
fetch c into en,s1,c1;
exit when c%notfound;
dbms_output.put(en);
if c1 is null then
c1:=0;
end if;
for i in 1..length(s1+c1)
loop
select word into word1 from words where
no=substr((s1+c1),i,1);
dbms_output.put(word1);
end loop;
dbms_output.put_line(' ');
end loop;
close c;
end;

4. List empname, manager name chain for each employee as follows


SMITH -------FORD ------ JONES ------ KING
declare
cursor c1 is
select empno,ename,mgr from emp;
eno emp.empno%type;
mno emp.mgr%type;
ena emp.ename%type;

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;

You might also like