Update Emp Set Sal Sal+sal 0.15 Where Empno &empno If Sql%found Then Dbms - Output - Put - Line ( Employee Record Modified')

You might also like

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

PL/SQL PROGRAMS

1 IMPLICIT CURSOR : USE OF %FOUND ATTRIBUTE


DECLARE

BEGIN

update emp set sal=sal+sal*0.15 where empno=&empno;

if sql%found then

dbms_output.put_line(‘Employee record modified’);

else

dbms_output.put_line(‘Employee no. does not exist’);

end if;

END;

2 –IMPLICIT CURSOR : USE OF %NOTFOUND ATTRIBUTE


DECLARE

BEGIN

update emp set sal=sal+sal*0.15

where empno=&empno;

if sql%notfound then

dbms_output.put_line(’employee no. does not exist’);

else

dbms_output.put_line(‘Employee record modified’);

end if;

end;

/
3 –IMPLICIT CURSOR : USE OF %ROWCOUNT ATTRIBUTE
/* WAB which accepts deptno from user and updates salary of all the employees of that department by 15%.
Display the message how many records are updated at the end. */

DECLARE

no number(2);

BEGIN

update emp set sal=sal+sal*0.15 where deptno=&deptno;

no :=sql%rowcount;

if sql%rowcount > 0 then

dbms_output.put_line( no ||’ Employee record modified’);

else

dbms_output.put_line(‘Dept no. does not exist’);

end if;

END;

EXPLICIT CURSORS

4 /* Display empno, salary end empname of all the emps from emp table.*/
declare

cursor c_emp is

select empno,sal,ename from emp;

eno emp.empno%type;

sal emp.sal%type;

enm emp.ename%type;

begin
open c_emp;

if c_emp%isopen then

loop

fetch c_emp into eno,sal;

exit when c_emp%notfound;

DBMS_OUTPUT.PUT_LINE(eno||’ ‘||sal||’ ‘||enm);

end loop;

commit;

close c_emp;

else

dbms_output.put_line(‘Unable to open Cursor’);

end if;

end;

5 /* WAB which accept the deptno from user and display eno, salary, empname of all the employees
working in that deptno. */
declare

cursor c_emp is

select empno,sal,ename from emp where deptno=&dno;

eno emp.empno%type;

sal emp.sal%type;

enm emp.ename%type;

begin
open c_emp;

if c_emp%isopen then

loop

fetch c_emp into eno,sal,enm;

exit when c_emp%notfound;

DBMS_OUTPUT.PUT_LINE(eno||’ ‘||sal||’ ‘||enm);

end loop;

commit;

close c_emp;

else

dbms_output.put_line(‘Unable to open Cursor’);

end if;

end;

6 /* WAB which accept department no from user and increment the salary of all the employees working in
that dept by 5%. */
— Use of %NOTFOUND

declare

cursor c_emp is

select empno,sal from emp where deptno=&deptno;

eno emp.empno%type;

sal emp.sal%type;

begin
open c_emp;

if c_emp%isopen then

loop

fetch c_emp into eno,sal;

exit when c_emp%notfound;

update emp set sal=sal+sal*0.05 where empno=eno;

end loop;

commit;

close c_emp;

else

dbms_output.put_line(‘Unable to open Cursor’);

end if;

end;

7 /* WAB which accept department no from user and increment the salary of all the employees working in
that dept by 5%. */
— Use of %FOUND

declare

cursor c_emp is select empno,sal from emp where deptno=&deptno;

eno emp.empno%type;

sal emp.sal%type;

begin

open c_emp;
loop

fetch c_emp into eno,sal;

if c_emp%found then

update emp set sal=sal+sal*0.05 where empno=eno;

commit;

else

exit;

end if;

end loop;

close c_emp;

end;

8 –WAB to print 4 highest salaried employees’ names and salaries.


declare

cursor c_emp is select empno,empname,sal from emp

order by sal desc;

eno emp.empno%type;

nm emp.empname%type;

sal emp.sal%type;

begin

open c_emp;

dbms_output.put_line(‘NAME EMP_NO Salary’);

loop
fetch c_emp into eno,nm,sal;

exit when c_emp%rowcount=4 or c_emp%notfound;

dbms_output.put_line(nm||’ ‘||eno||’ ‘||sal);

end loop;

close c_emp;

end;

9 /* WAB which accept the deptno from user and display eno, salary, empname of all the employees
working in that deptno. */
— Demo of cursor for loop.

declare

cursor c_emp is

select empno,ename,sal from emp where deptno=&deptno;

begin

for c in c_emp

loop

dbms_output.put_line(c.empno||’ ‘||c.ename||’ ‘||c.sal);

end loop;

end;

/* WAB which inserts all the students living in Rajkot from table emp into table stud_Rajkot. */
DECLARE

CURSOR c_student IS SELECT student_id, last_name, first_name

FROM student WHERE city= ‘Rajkot’;


BEGIN

FOR x IN c_student

LOOP

INSERT INTO stud_Rajkot VALUES(x.last_name);

END LOOP;

END;

10 — Demo of cursor for loop.


declare

cursor c_emp is

select empno,sal from emp where deptno=&deptno;

begin

for c in c_emp

loop

update emp set sal =c.sal+c.sal*0.05 where empno=c.empno;

–insert into emp_raise values(c.empno,sysdate,c.sal*0.05);

end loop;

commit;

end;

11 — Write a block using cursor which display the sum of salary department wise.
declare

cursor c1 is select deptno,sum(sal) tot from emp group by deptno;

–rec c1%rowtype;

sal emp.sal%type;
begin

for rec in c1

loop

dbms_output.put_line(‘Deptno : ‘||rec.deptno||’ sum of salary : ‘||rec.tot);

dbms_output.put_line(‘ ‘);

end loop;

select sum(sal) into sal from emp;

dbms_output.put_line(‘total salary of all the department : ‘||sal);

end;

12 /* WAB which accept city from user and display empno and name employees living in that city. */
declare

cursor c1(xcity varchar2) is

select * from emp where city=xcity order by city;

rec c1%rowtype;

tcity emp.city%type;

begin

tcity:=’&city’;

for rec in c1(tcity)

loop

dbms_output.put_line(’empno : ‘||rec.empno||’ empname : ‘||

rec.empname||’ city : ‘||rec.city);

dbms_output.put_line(‘ ‘);
end loop;

end;

13 /* Use of parameterized cursor. Write a PL/SQL block which check for the existence
of deptno. if exist then enter the record for employee into emp table.*/

declare

cursor c2(xdno number) is

select deptno from dept where deptno=dno;

d_no number(4);

eno number(5);

nm varchar2(15);

begin

eno :=&empno;

nm:=’&empname’;

d_no:=&deptno;

open c2(d_no);

fetch c2 into d_no;

if c2%found then

insert into emp(empno,empname,deptno) values(eno,nm,d_no);

else

dbms_output.put_line(‘deptno ‘||d_no|| ‘ does not exist’);

end if;

close c2;
end;

14 –display the sum of salary department wise using cursor.


declare

cursor c1 is select distinct deptno,dname from dept;

cursor c2(xdeptno number) is select sum(sal) from emp group by deptno having deptno=xdeptno;

dno number;

sal emp.sal%type;

begin

for i in c1 loop

–dno:=c1.deptno;

dbms_output.put_line(i.deptno||’: ‘||i.dname);

dno:=i.deptno;

open c2(dno);

fetch c2 into sal;

dbms_output.put_line(‘SUM of salary: ‘||sal);

close c2;

end loop;

end;

You might also like