0% found this document useful (0 votes)
33 views

Assignment 2

1. The document describes an assignment to write a PL/SQL procedure to increase employee salaries stored in an EMP table based on their grade. 2. The procedure uses a cursor to fetch each employee's grade, salary, and ID. It then checks the grade and increases the salary by £500 or £1000 depending on the grade, updating the table. 3. The second question amends the procedure to check if the increased salary would exceed the maximum for that employee's grade, stored in a new GRADE table, and sets the salary to the maximum if so.

Uploaded by

abilashaanair99
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Assignment 2

1. The document describes an assignment to write a PL/SQL procedure to increase employee salaries stored in an EMP table based on their grade. 2. The procedure uses a cursor to fetch each employee's grade, salary, and ID. It then checks the grade and increases the salary by £500 or £1000 depending on the grade, updating the table. 3. The second question amends the procedure to check if the increased salary would exceed the maximum for that employee's grade, stored in a new GRADE table, and sets the salary to the maximum if so.

Uploaded by

abilashaanair99
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

CC301 Database Technology

BIC2323 Database Technology

Student Name Abilashaa Nair


Student ID 1001955469
Lecturer Name Dr. Chloe Chee Ling Thong
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
CC301 Database Technology

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.

CREATE OR REPLACE PROCEDURE salary_increase AS


Declare the cursor and variables here without DECLARE
BEGIN

Open the cursor

LOOP
FETCH complete the fetch command
EXIT WHEN cursor name%NOTFOUND; /*this will exit when cursor empty*/

Check using if statement if the grade is < 4 if so increase salary by 500


Check using if statement if the grade is > 3 if so increase salary by 1000

END LOOP;
END;

Once this is completed compile the code and correct any errors if any are noted.

Execute the procedure and note the changes to the salary.

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

create table emp(


Empid number,
Empname varchar2(50),
Empaddr1 varchar2(50),
Empaddr2 varchar2(50),
Empaddr3 varchar2(50),
Emppostcode varchar(10),
Empdept varchar2(20),
Empgrade number,
Empsalary number,
constraint pk_emp primary key (Empid)
)

insert into emp


values(345, 'KIREN', 'Jalan', 'Silang' , 'Ipoh', 30594, 'Assistant Manager', 5, 7900)

insert into emp


values(124, 'RAMESH', 'Jalan', 'Prima' , 'Saujana', 43000, 'System Analyst', 4, 7500)

insert into emp


values(241, 'ANUSHA', 'Jalan', 'Setia' , 'Maju', 42100, 'Developer', 5, 7800)

insert into emp


values(142, 'SHEKY', 'Jalan', 'Angsa' , 'Maju', 40400, 'Sales', 2, 5000)

insert into emp


values(134, 'ABBY', 'Hijayu', 3 , 'Sendayan', 71950, 'Manager', 6, 8500)

insert into emp


values(352, 'BANU', 'Jalan', 'Muhhibah' , 'Shah Alam', 40400, 'Accounts Manager', 4, 7500)

insert into emp


values(389, 'AVI', 'Jalan', 'Melati' , 24, 68100, 'Researcher', 3, 6000)

insert into emp


values(238, 'KEVIN', 'Jalan', 'Semangat' , 'Muda', 40400, 'Developer', 5, 7800)

insert into emp


values(567, 'KUMS', 'Jalan', 'Ceria' , 7, 73200, 'Clerk', 1, 2300)

insert into emp


values(350, 'OOI', 'Jalan', 'Cove' , 'Klang', 42000, 'Clerk', 1, 2300)

SELECT * FROM emp


CC301 Database Technology

CREATE OR REPLACE PROCEDURE salary_increase IS


s_Empid emp.Empid%TYPE;
s_Empgrade emp.Empgrade%TYPE;
s_Empsalary emp.Empsalary%TYPE;
CURSOR c_Emp is Select Empid,Empgrade,Empsalary from emp;
BEGIN
dbms_output.put_line(lpad('s_Empid',8)||lpad('s_Empgrade',15)||lpad('OldSalary',15)||
lpad('NewSalary',15));
OPEN c_Emp;
LOOP
FETCH c_Emp into s_Empid,s_Empgrade,s_Empsalary;
EXIT WHEN c_Emp%notfound;
IF ( s_Empgrade < 4 ) THEN
update emp set Empsalary=Empsalary+500 where Empid=s_Empid;
dbms_output.put_line(lpad(s_Empid,8)||lpad(s_Empgrade,15)||lpad(s_Empsalary,15)||
lpad(s_Empsalary+500,15));
ELSE
update emp set Empsalary=Empsalary+1000 where Empid=s_Empid;
dbms_output.put_line(lpad(s_Empid,8)||lpad(s_Empgrade,15)||lpad(s_Empsalary,15)||
lpad(s_Empsalary+1000,15));
END IF;
END LOOP;
CLOSE c_Emp;
END;
/
EXECUTE salary_increase()

Before increasing salary:


CC301 Database Technology

After increasing salary:

Question 2

CREATE TABLE Grade (Grade_id number, salary_Min number, salary_Max number);


CC301 Database Technology

INSERT INTO Grade VALUES('1', '10000', '15000');


INSERT INTO Grade VALUES('2', '14000', '19500');
INSERT INTO Grade VALUES('3', '19000', '22000');
INSERT INTO Grade VALUES('4', '21500', '25500');
INSERT INTO Grade VALUES('5', '25000', '30000');

CREATE OR REPLACE PROCEDURE salary_increase AS


CURSOR e_record IS
SELECT Empgrade,Empsalary,Empid,salary_Max
FROM emp, Grade
WHERE emp.Empgrade = Grade.Grade_id;
e_grade NUMBER;
e_sal NUMBER;
e_id NUMBER;
sal_Max NUMBER;
BEGIN
OPEN e_record;
LOOP
FETCH e_record INTO e_grade,e_sal,e_id,sal_Max;
EXIT WHEN e_record%NOTFOUND;
IF e_grade < 4 THEN
IF (e_sal+500) > sal_Max THEN
UPDATE emp
SET Empsalary=sal_Max
CC301 Database Technology

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()

You might also like