Assignment 2
Assignment 2
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:
4 21500-25500
5 25000-30000
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.
LOOP
FETCH complete the fetch command
EXIT WHEN cursor name%NOTFOUND; /*this will exit when cursor empty*/
END LOOP;
END;
Once this is completed compile the code and correct any errors if any are noted.
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).
Question 1
CC301 Database Technology
Question 2
WHERE Empid=e_id;
ELSE
UPDATE emp
SET Empsalary=Empsalary+500
WHERE Empid=e_id;
END IF;
ELSE
IF (e_sal+1000) > sal_Max THEN
UPDATE emp
SET Empsalary=sal_Max
WHERE Empid=e_id;
ELSE
UPDATE emp
SET Empsalary=Empsalary+1000
WHERE Empid=e_id;
END IF;
END IF;
END LOOP;
END;
/
EXECUTE salary_increase()