You are on page 1of 12

PL/SQL ASSIGNMENT-2

I Programs using cursors:

1.Write a pl/sql program using cursor for getting user entered empno and display the name and
salary of the employee

A. Implicit cursor

declare

x emp%rowtype;

begin

select * into x from emp where empno=:A;

DBMS_OUTPUT.PUT_LINE('The Employee name is ' ||x.ENAME||' with salary of '||x.SAL);

end;

B.Explicit cursor

declare

cursor c1 is select ename,sal from emp where empno=:V_EMPNO;

V_NAME varchar2(25);

V_SAL varchar2(25);

begin

open c1;

fetch c1 into V_NAME,V_SAL;

DBMS_OUTPUT.PUT_LINE('The Employee name is ' ||V_NAME||' with salary of '||V_SAL);

close c1;

end;

=======================================================================

2.W.A.P. to display all record from emp table using explicit cursor lifecycle

A.

declare

cursor c1 is select * from emp;

i emp%rowtype;

begin
open c1;

loop

fetch c1 into i;

DBMS_OUTPUT.PUT_LINE(I.EMPNO||','||I.ENAME||','||I.MGR||','||I.SAL||','||I.JOB||','||
I.COMM||','||I.DEPTNO);

exit when c1%notfound;

end loop;

close c1;

end;

3.W.A.P using cursor to display first 5 highest salary from emp table using % rowcount attribute.

A. declare

cursor c1 is select * from emp order by sal desc;

i emp%rowtype;

begin

open c1;

loop

fetch c1 into i;

exit when c1%rowcount>5;

DBMS_OUTPUT.PUT_LINE(i.empno||','||i.ename||','||i.sal);

end loop;

close c1;

end;

4.W.A.P using cursor to display maximum salary from emp table.

A.

5.W.A.P to display even number of rows from emp table using %rowcount attribute

A.

create table s1 as select * from (select ename,job, ROWNUM AS rn from emp) where mod(rn, 2) = 0;
declare

cursor c1 is select * from (select ename,job, ROWNUM AS rn from emp) where mod(rn, 2) = 0;

i s1%rowtype;

begin

open c1;

loop

fetch c1 into i;

exit when c1%notfound;

end loop;

DBMS_OUTPUT.PUT_LINE('Total number of records is :'||c1%rowcount);

close c1;

end;

6.W.A.P to display records from emp where salary<2000

A.

declare

cursor c1 is select * from emp where sal<2000;

begin

for I in c1 loop

DBMS_OUTPUT.PUT_LINE(I.EMPNO||','||I.ENAME||','||I.MGR||','||I.SAL||','||I.JOB||','||
I.COMM||','||I.DEPTNO);

end loop;

end;

7.W.A.P using parameter cursor to display employee working as manager or analyst from emp table

A.

exit when c1%notfound;


8.W.A.P to retrieve deptno from dept table and pass this cursor into another cursor to retrieve
employee details

A.

declare

cursor c1 is select deptno from dept;

cursor c2(V_DEPTNO number) is select * from emp where deptno= V_DEPTNO;

begin

for I in c1

loop

for j in c2(i.deptno)

loop

DBMS_OUTPUT.PUT_LINE(J.EMPNO||','||J.ENAME||','||J.MGR||','||J.SAL||','||J.JOB||','||
J.COMM||','||J.DEPTNO);

end loop;

end loop;

end;

9.W.A.P to alter salary of emp based on conditions

(i)job=clerk then salary +100

(ii)job=salesman then salary +200

(iii)job=analyst then salary +100

declare

cursor c1 is select * from emp1;

begin

for I in c1

loop

if i.job='CLERK' then

update emp1 set sal=i.sal+100 where empno=i.empno;

elsif i.job='SALESMAN' then

update emp1 set sal=i.sal+200 where empno=i.empno;

elsif i.job='ANALYST' then


update emp1 set sal=i.sal+100 where empno=i.empno;

end if;

end loop;

end;

II EXERCISE ON PROCEDURES

1. W.A.P for passing empno as parameter into procedure and display NAME,SALARY from emp

A.

create or replace procedure P_INSERTION(V_EMPNO number) is

v_name varchar2(25);

v_salary number(5);

begin

select ename,sal into v_name,v_salary from emp where empno= V_EMPNO;

exception

when others then

V_NAME:='NULL';

V_SALARY:=0;

DBMS_OUTPUT.PUT_LINE('The name of the employee is '||v_name||' with a salary of '||


v_salary);

end;2.W.A.P for passing deptno as a parameter to display emp details

A.

create or replace procedure P_EMP_DETAILS(V_DEPTNO number) is

cursor c1 is select * from emp where deptno= V_DEPTNO;

begin

for I in c1 loop DBMS_OUTPUT.PUT_LINE(I.EMPNO||','||I.ENAME||','||I.MGR||','||


I.SAL||','||I.JOB||','||I.COMM||','||I.DEPTNO);

end loop;

end;

3.W.A.P for passing emp name as a parameter and return salary of employee using out
parameter from emp

A.

create or replace procedure P_SALARY(V_ENAME varchar2


,V_SAL out number)

is

begin

select sal into V_SAL from emp where empname= V_ENAME;

exception

when others then

V_SALARY:=0;

DBMS_output.put_line('The salary is '||V_SAL);

end;

4.W.A.P for passing deptno as a parameter and display no of department

A.create or replace procedure P_NO_OF_DEPARTMENTS(V_DEPTNO number) is

C number(3);

begin

select count(*) into C

from emp where deptno= V_DEPTNO

group by deptno;

DBMS_output.put_line('The count is '||c);

end;

5.Enter 4 columns into emp table and use pragma autonomous transactions

A. create table location (country varchar2(25));

create or replace procedure P_PARENT is

pragma autonomous_transaction;

begin

insert into location values ('America');

commit;

end;

create or replace procedure P_CHILD is


begin

insert into location values ('United states');

insert into location values ('India');

insert into location values (‘Australia’);

p;

rollback;

end;

Programs on Functions

1 W.A.P for passing a number and return even or odd

A. create or replace function F_EVEN(V_NUM number)

return varchar2

is

begin

if mod(V_NUM,2)=0 then

return'Even number';

else

return'Odd number';

end if;

end;

2.W.A.P for passing empno from emp and return gross salary of the employee

A.

create or replace function F_SAL(V_EMPNO number)

return number

is

gross number;

salary number;

hra number;

df number;

pf number;

begin
select sal into salary from emp where empno= V_EMPNO;

hra:=salary*0.1;

df:=salary*0.2;

Pf:=salary*0.1;

gross:=(salary+hra+df-pf);

return gross;

end;

3.W.A.P passing empno and date as parameter and return no of years that member is working
based on days

A.

create or replace function F_NO_OF_YEARS(V_EMPNO number

,B date)

return number

is

i number;

begin

select months_between(B,hiredate)/12 into I from emp where empno= V_EMPNO;

return I;

end;

4.W.A.P to pass empno and calculate tax based on conditions from employee

(i) if sal>1000o then tax 10%

(ii) if sal>15000 then tax 20%

(iii) sal>20000 then tax 30%

A. create or replace function F_TAX(V_EMPNO number)

return number

is

vsal number;

asal number;
itax number;

begin

select sal into vsal from emp where empno= V_EMPNO;

asal:=vsal*12;

if asal>10000 and asal<=15000 then

itax:=asal*0.1;

elsif asal>15000 and asal<=20000 then

itax:=asal*0.2;

elsif asal>20000 then

itax:=asal*0.3;

else

itax:=0;

end if;

return itax;

end;

Programs on Triggers

1.row level trigger-if salary<5000

A. create or replace trigger T_SALARY

before insert on emp

for each row

begin

if :new.sal<5000 then

raise_application_error(-20139,' Salary should be greater than 5000');

end if;
end;

2.W.A.T using emp, dept table and implement on delete cascading without using delete clause

A. create or replace trigger T_CASACADING

after delete on dept

for each row

begin

delete from emp where deptno=:old.deptno;

end;

3.W.A.T. on dept table whenever dept no into dept table automatically emp table also get
changed

A. create or replace trigger T_DELETE_AUTOMATION

after update on dept

for each row

begin

update emp set deptno=:new.deptno where deptno=:old.deptno;

end;

4.W.A.T whenever user enter data into ename column should be in upper case

A. create or replace trigger T_UPPER

before insert on emp

for each row

begin

if :new.ename=lower(:new.ename) then

raise_application_error(-20139,'Name should be in uppercase');

end if;

end;

5.W.A.T on emp not to perform DML operation on Satday and Sunday

A. create or replace trigger T_WEEKEND


before insert or update or delete 

on departments

begin

if to_char(sysdate,'DY') in ('SAT','SUN')

then

raise_application_error(-20123,'We cannot perform this function on weekends');

end if;

end;

6. W.A.T on emp not to perform DML operation on last day of month

A create or replace trigger T_LASTDAY

before insert or update or delete

on emp

begin

if sysdate=last_day(sysdate)

then

raise_application_error(-20123,'We cannot perform this function on last day of the month');

end if;

end;

7.W.A.T. on emp table when user insert data values are stored in another table

(i) UPDATE

(ii) DELETE

A.

create or replace trigger T_DATA_IN_ANOTHER_TABLE

after update or delete on emp

for each row

begin

if updating then

insert into d1(empno,ename) values (:old.empno,:old.ename);

elsif deleting then

insert into d2(empno,ename) values (:old.empno,:old.ename);


end if;

end;

You might also like