You are on page 1of 3

Ques-1> create trigger which will be fire automatically as we insert the record to

the employees table(name,code,doj,salary,dop), it other calculation has been made


and store to the pay,deduction,final table, the calculation is as follows,

da 50%of salary
hra 40%of salary
pf 5%of salary
gross salary+da+hra
netpay gross-pf
also use exception
CODE---

CREATE TRIGGER Update_Employees


BEFORE INSERT ON Employees

DECLARE
e_code Employees.code%type;
e_salary Employees.salary%type;
p_da pay.da%type;
p_hra pay.hra%type;
d_pf deduction.pf%type;
f_gross final.gross%type;
f_netpay final.netpay%type;

BEGIN
SELECT salary code from Employees;
EXCEPTION
EXIT WHEN salary%Notfound
dbms_output.put_line("record not found");

WHEN salary%Found THEN


e_salary= Employees.salary;
p_da=(e_salary*50)/100;
p_hra=(e_salary*40)/100;
d_pf=(e_salary*5)/100;
f_gross=e_salary+p_da+p_hra;
f_netpay=f_gross-f_pf;
dbms_output.put_line("salary"||e_salary||"da"||p_da||"hra"||p_hra||"pf"||
d_pf||"gross"||f_gross||"netpay"||f_netpay);
BEGIN
INSERT ALL
INSERT INTO pay VALUES(code,da,hra);
INSERT INTO deduction VALUES(code,pf);
INSERT INTO final VALUES(code,gross,netpay);
END;

END;
/

DECLARE
e_name Employees.name%type;
e_code Employees.code%type;
e_doj Employees.doj%type;
e_salary Employees.salary%type;
e_dop Employees.dop%type;
BEGIN
e_name="saurabh";
e_code=3;
e_doj=TO_DATE('9/5/2020','DD/MM/YYYY');
e_salary=40000;
e_dop=TO_DATE('9/5/2020','DD/MM/YYYY');
BEGIN

INSERT INTO Employees VALUES(e_name,e_code,e_doj,e_salary,e_dop);


END;
END;

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

Ques- 2 > wap to calculate the total salary give to the employee to the current
year using cursor,also use exception.

DECLARE
e_salary Employees.salary%type;
p_da pay.da%type;
p_hra pay.hra%type;
total_salary number(10);
Calculate_Salary varchar2(1000);

CURSOR Calculate_salary IS

SELECT E.e_salary salary P.p_da da P.p_hra hra FROM


Employees E Natural Join pay P;

BEGIN

BEGIN
Calculate_Salary:='
create table Employee_Salary
(
e_salary number(5),
p_da real(5,2),
p_hra real(5,2),
total_salary real(10,2),
)';
execute immediate Calculate_Salary;
commit;
END;

OPEN Calculate_salary;

LOOP

FETCH Calculate_salary into e_salary p_da p_hra;

EXCEPTION
WHEN Calculate_salary%notfound THEN
dbms_output.put_line('No more records');

total_salary=e_salary+p_da+p_hra;

dbms_output.put_line(total_salary);

BEGIN
execute immediate INSERT INTO Employee_Salary VALUES(e_salary, p_da, p_hra,
total_salary);

END;

close LOOP;
close Calculate_salary;
END;
==========================================

Ques-3> wap to count that how many employees are in the employees table using
cursor, also use exception.

DECLARE
total_Employee Number(3);

CURSOR Employee IS
select * from Employees;

BEGIN
OPEN Employee;

EXCEPTION
WHEN sql%notfound THEN
dbms_output.put_line("record finished");

WHEN sql%found THEN

total_Employee= Employee%ROWCOUNT;

dbms_output.put_line("Total_Employees"||total_Employee);

CLOSE Employee;
END;

You might also like