You are on page 1of 6

CC301 Database Technology

Assignment 2:
Writing Cursors

PL/SQL CURSORS

In order to perform the following exercise you will need to create a table detailed below:

EMPLOYEE
Empid number
Empname varchar2(50)
Empaddr1 varchar2(50)
Empaddr2 varchar2(50)
Empaddr3 varchar2(50)
Emppostcode varchar2(10)
Empdept varchar2(20)
Empgrade number
Empsalary number

The purpose of this exercise is to create a procedure which, when evoked, will increase
the salary of a grade by £500 or £1000 depending on the level of the grade. You will
need to populate the table first with a minimum of 10 entries, the empgrade value should
be between 1 and 5, and the idea is the higher the grade the higher the manager.
The code below will create the procedure but you will need to fill in the missing bits
indicated by BOLD letters. Once you have done this you should compile the code by
entering @salary_increase at your SQL prompt once logged on. This will then indicate
if the procedure has compiled or not. If compilation is unsuccessful entering the
command show errors will list any problems with the code. To run the procedure you
type execute salary_increase at the SQL prompt.

NOTES:
Remember that you need to have a variable to store each component of the return from
the cursor.
Where an IF statement is used remember to END IF;
The salaries for the grades are listed below:

Grade Salary Range


1 10000-15000
2 14000-19500
3 19000-22000
4 21500-25500
5 25000-30000
CC301 Database Technology

Solution:
CREATE TABLE EMPLOYEE(

Empid number,

Empname varchar2(50),

Empaddr1 varchar2(50),

Empaddr2 varchar2(50),

Empaddr3 varchar2(50),

Emppostcode varchar2(10),

Empdept varchar2(20),

Empgrade number,

Empsalary number

);

INSERTING DATA INTO TABLE


INSERT Into EMPLOYEE Values(2,'Anjali Gurung','Nakkhu', 'Kalimati', 'Pokhara',
'44700', 'Frontend Developer', 7, 82000);

INSERT Into EMPLOYEE Values(3,'Jyoti Gurung','Lagankhel', 'Kumaripati', 'Chitwan',


'46500', 'JS Developer', 4, 54000);

INSERT Into EMPLOYEE Values(4,'Urmila Shahi Thakuri','Bagdole', 'Dhading',


'Bharatpur', '46000', 'Full Stack Developer', 1, 120000);

INSERT Into EMPLOYEE Values(5,'Budhha Lama','Sundhara', 'Mangal Bazar', 'Iilam',


'92800', '.Net Developer', 3, 46000);

INSERT Into EMPLOYEE Values(6,'Jwala Bista','Newroad', 'Basantapur', 'Jhapa',


'77700', 'Andriod Developer', 2, 70000);
CC301 Database Technology

INSERT Into EMPLOYEE Values(7,'Vusan Pudaisaini','Thimi', 'Sanothimi',


'Makwanpur', '96000', 'Quality Assurance', 4, 65000);

INSERT Into EMPLOYEE Values(8,'Reshav Thakur','Kupondole', 'Bhaisipati',


'Dhnakuta', '24700', 'PhP Developer', 8, 62000);
INSERT Into EMPLOYEE Values(9,'Sreejak Maharjan','Putalisadak', 'Baneswor',
'Dharan', '43700', 'Frontend Developer', 10, 93000);
INSERT Into EMPLOYEE Values(10,'Sobita kandel','Sanepa', 'Nayabato', 'Chuchepati',
'84700', 'NodeJs Developer', 6, 84000);

Select * from EMPLOYEE;

Table
CC301 Database Technology

Question 1:

Complete the code below, you should include a cursor which will select the grade, salary
and employee id for each record. You should then use this information to check the
grade and increase salary accordingly. The criteria for increase is given in the procedure
below.
Answer:
CREATE OR REPLACE PROCEDURE salary_increase AS
employee_GRADE EMPLOYEE.EMP_GRADE%TYPE;
employee_SALARY EMPLOYEE.EMP_SALARY%TYPE;
employee_ID EMPLOYEE.EMP_ID%TYPE;
CURSOR EMP_CUR IS SELECT EMP_GRADE, EMP_SALARY, EMP_ID FROM
EMPLOYEE;
BEGIN
OPEN EMP_CUR;
LOOP
FETCH EMP_CUR INTO employee_GRADE, employee_SALARY, employee_ID;
EXIT WHEN EMP_CUR%NOTFOUND;
If (employee_GRADE < 4) THEN
employee_SALARY := employee_SALARY+500;
ELSIF (employee_GRADE > 3) THEN
employee_SALARY := employee_SALARY+ 1000;
END IF;
UPDATE EMPLOYEE SET EMP_SALARY = employee_SALARY WHERE
EMP_ID = employee_ID;
COMMIT;
END LOOP;
CLOSE EMP_CUR;
END;
/
CC301 Database Technology

Question 2:

Amend the code so that if the salary increase takes the salary above the maximum for that
grade the salary is altered to the maximum for that grade and not increased above that.
(NOTE: you need to create a grade table for this task).

Answer:

CREATE OR REPLACE PROCEDURE salary_increase AS


employee_GRADE EMPLOYEE.EMP_GRADE%TYPE;
employee_SALARY EMPLOYEE.EMP_SALARY%TYPE;
employee_ID EMPLOYEE.EMP_ID%TYPE;
employee_SAL_MAX NUMBER
CURSOR EMP_CUR IS SELECT EMP_GRADE, EMP_SALARY, EMP_ID FROM
BEGIN
OPEN EMP_CUR;
LOOP
FETCH EMP_CUR INTO employee_GRADE, employee_SALARY,
employee_ID;
EXIT WHEN EMP_CUR%NOTFOUND;
If (employee_GRADE < 4) THEN
employee_SALARY := employee_SALARY+500;
ELSIF (employee_GRADE > 3) THEN
employee_SALARY := employee_SALARY+ 1000;
END IF;
SELECT MAXSALARY INTO employee_SAL_MAX FROM Grade WHERE grade=
employee_GRADE;
IF employee_SALARY > employee_SAL_MAX THEN
employee_SALARY:= employee_SAL_MAX;
END IF;

UPDATE EMPLOYEE SET EMP_SALARY = employee_SALARY WHERE EMP_ID


= employee_ID;
COMMIT;
CC301 Database Technology

END LOOP;
CLOSE EMP_CUR;
END;
/

You might also like