PL/SQL

Hands On No. 8 : Working with Multiple Tables using cusror in PL/SQL

Working with multiple tables using cursor
In this practical, we will deal with two tables using one cursor. First table employee, will store the information about employees and it's basic pay details while another table will store the information about salary details of employee which includes commission+basicpay.
Commission will be given according to the Basic Pay on following criteria :
Basicpay > 28000 commission = Basicpay * 0.20
Basicpay > 22000 commission = Basicpay * 0.15
Otherwise commission = Basicpay * 0.10
Table Schema of employee
create table employee ( 
    eno number constraint employee_pk primary key, 
    ename varchar2(50), 
    ebasic number, 
    dept varchar2(50) 
)
INSERT INTO employee(eno, ename,ebasic,dept) VALUES(1,'Dhaval Mehta',30000,'BCA')
INSERT INTO employee(eno, ename,ebasic,dept) VALUES(2,'Yash Karanke',20000,'BCA')
INSERT INTO employee(eno, ename,ebasic,dept) VALUES(3,'Hemal Shah',25000,'CSE')
INSERT INTO employee(eno, ename,ebasic,dept) VALUES(4,'Aakash Gupta',22000,'IT')

Table Schema of employee_salary
create table employee_salary (  
    eno number,  
    ename varchar2(50),  
    commission number 
)

Cursor which calculates Salary
DECLARE  
  eno employee.eno%type; 
  ename employee.ename%type;
  ebasic employee.ebasic%type;
  total float;
  commission float;
  CURSOR cdetails is  
      SELECT eno, ename,ebasic FROM employee;  
BEGIN
   delete from employee_salary;
   OPEN cdetails;  
   LOOP
      total := 0;
      FETCH cdetails into eno,ename,ebasic;  
      EXIT WHEN cdetails%notfound;  
      IF ebasic >28000 THEN
        commission := ebasic * 0.20;
      ELSIF ebasic >22000 THEN
        commission := ebasic * 0.15;
      ELSE
        commission := ebasic * 0.10;
      END IF;
      total := ebasic +  commission;
      insert into employee_salary values(eno,ename,total);
      DBMS_OUTPUT.PUT_LINE('Employee Name : ' || ename || ' ->Basic Salary : ' || ebasic || ' ->Commission : ' || commission ||' ->Total : ' || total );
    END LOOP;
        
   CLOSE cdetails; 
 
END;