I have defined the following procedure successfully in my database, but somehow it doesn’t work as expected.
CREATE PROCEDURE `mod_prefer`(IN sid INT)
BEGIN
declare s1 int;
declare s2 int;
SELECT SUM(c_load) INTO s1 FROM course WHERE id IN (SELECT course_id FROM prefer WHERE staff_id = sid) AND sem = 1;
SELECT SUM(c_load) INTO s2 FROM course WHERE id IN (SELECT course_id FROM prefer WHERE staff_id = sid) AND sem = 2;
IF s1>s2 THEN
UPDATE staff SET sem = 1 WHERE id = sid;
ELSE
UPDATE staff SET sem = 2 WHERE id = sid;
END IF;
END
The above procedure is called using a trigger as follows (it’s an after insert trigger):
CALL mod_prefer(new.staff_id);
The result is that instead of setting the sem value for the given staff member, the procedures updates all sem values to 2, irrespective of how the logic should work i.e. even if the value for sem should actually be 1.
Please Help.
Try this code –