You are on page 1of 5

11 a:

AIM:

Write a block in PL/SQL to create a Cursor that displays the employee name and number of jobs he or
she has done in the past.

PROCEDURE

1. Create a employee table with id, name, past work, present work
2. Create an explicit courser
3. Write select statements within courser
4. Open , Fetch and Close courser

PL/SQL COMMADS :

create table Employee_table(emp_id number(4) primary key, emp_name varchar2(30) , emp_pastwork


varchar2(20), emp_presentwork varchar2(20));

insert into Employee_table values(123 , 'Althaf' , 'Clerk', 'Manager');

insert into Employee_table values(121, 'Mahesh', 'Engineer', 'Team Lead');

insert into Employee_table values(122, 'AbhiRam', 'Team Lead', 'Cheif Executive');

insert into Employee_table values(141, 'Sree', 'Architect', 'Contractor');

select * from Employee_table;

DECLARE

empId number(4);

empName varchar2(30);

empPastWork varchar2(30);

--creating external courser

--sql creates cursor for each query implicitly

--now creating explicit cursor

CURSOR Past_Works IS

SELECT emp_id ,emp_name, emp_pastwork

from Employee_table;

--CURSOR CONTAINS THREE OPERATIONS NAMELY OPEN ,FETCH, CLOSE

BEGIN

OPEN Past_Works;
LOOP

FETCH Past_Works INTO empId, empName, empPastWork;

DBMS_OUTPUT.PUT_LINE(empId || ' ' || empName ||' '||empPastWork );

EXIT WHEN Past_Works%NOTFOUND;

END LOOP;

CLOSE Past_Works;

END;

OUTPUT:

Table created

EMP_ID EMP_NAME EMP_PASTWORK EMP_PRESENTWORK

123 Althaf Clerk Manager

121 Mahesh Engineer Team Lead

122 AbhiRam Team Lead Cheif Executive

141 Sree Architect Contractor

4 rows selected.

Statement processed.
123 Althaf Clerk
121 Mahesh Engineer
122 AbhiRam Team Lead
141 Sree Architect
141 Sree Architect

11b:
Aim:
To Write a program in PL/SQL to create a cursor to display the name and salary of each
employee in the EMPLOYEES table whose salary is less than that specified by a passed-in
parameter value.
PROCEDURE:
1. Create a Employee table with name, salary.
2. Create a explicit courser with parameter
3. Take dynamic input of salary
4. Filter the result salary of people less than mentioned salary
5. Then Open , Fetch and Close the courser
PL/SQL COMMANDS:
aa create table Employee_sal(emp_id number(4) primary key, emp_name varchar2(30) ,emp_sal
number(10));

insert into Employee_sal values(123 , 'Althaf' , 10000);

insert into Employee_sal values(121, 'Mahesh', 8999);

insert into Employee_sal values(122, 'AbhiRam', 8972);

insert into Employee_sal values(141, 'Sree', 11000 );

select * from Employee_sal;

--PL/SQL:

DECLARE

empDesired_sal number(10);

empName varchar2(30);

empSalary NUMBER(10);

--creating external courser

--sql creates cursor for each query implicitly

--now creating explicit cursor with parameter

CURSOR Sal_filter(sal NUMBER) IS

SELECT emp_name, emp_sal

from Employee_sal

where emp_sal < sal;

--CURSOR CONTAINS THREE OPERATIONS NAMELY OPEN ,FETCH, CLOSE

BEGIN

empDesired_sal:= 10000;

OPEN Sal_filter(empDesired_sal);
DBMS_OUTPUT.PUT_LINE( 'Employees of salary below '|| empDesired_sal ||' are:');

LOOP

FETCH Sal_filter INTO empName, empSalary;

DBMS_OUTPUT.PUT_LINE( empName ||' '||empSalary );

EXIT WHEN Sal_filter%NOTFOUND;

END LOOP;

CLOSE Sal_filter;

END;

OUTPUT:

EMP_ID EMP_NAME EMP_SAL

123 Althaf 10000

121 Mahesh 8999

122 AbhiRam 8972

141 Sree 11000

4 rows selected.
Statement processed.
Employees of salary below 10000 are:
Mahesh 8999
AbhiRam 8972
AbhiRam 8972

You might also like