-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocedures.sql
More file actions
31 lines (26 loc) · 901 Bytes
/
procedures.sql
File metadata and controls
31 lines (26 loc) · 901 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
delimiter //
CREATE PROCEDURE calculate_grades (
IN module CHAR(6),
IN student INTEGER
)
MODIFIES SQL DATA
BEGIN
DECLARE mark FLOAT;
SELECT SUM(grade)
INTO mark
FROM exam_grades
WHERE exam_grades.student = student
AND
exam IN(SELECT id FROM exams WHERE exams.module = module);
IF mark IS NULL THEN
SET mark = 0;
END IF;
IF (SELECT COUNT(*) FROM module_grades WHERE module_grades.module = module AND module_grades.student = student) = 0 THEN
INSERT INTO module_grades (module, student, mark) VALUES (module, student, mark);
ELSE
UPDATE module_grades
SET module_grades.mark = mark, date_recorded = NOW()
WHERE module_grades.module = module AND module_grades.student = student;
END IF;
END;//
delimiter ;