Basically i’m going to run this procedure anytime a student enrolls/drops a course.
I’m trying to set student_total = # of students in a course, then update that corresponding section with (student_total + 1) i’m having trouble finding good documentation for stored procedures. I’m getting an error on my Declare student_total int; line. What am i not doing correct?
DELIMITER $$
CREATE PROCEDURE `mydb`.`update_seats` (IN section_ID varchar(20))
BEGIN
SET @section_id=section_id;
DECLARE student_total int;
-- count the number of students in the course --
SET student_total = SELECT count(student_ID) from course
WHERE section_ID = @section_id;
Update Section SET students_enrolled = (student_total + 1)
WHERE section_ID = @section_id;
END
Problems
Error 1
From MySql documentation:
So, you should move
DECLARE...statement before theSET @section_id...statement.Error 2
You are trying to select a value into a variable using invalid snytax! You should use
SELECT ... INTOinstead ofSET ... = SELECT ...(which is invalid syntax).Removing Redundancy
No need to assign parameter (
section_ID) to a global variable (@section_ID). You can simply change the parameter name to avoid name collision withsection.section_IDcolumn.Solution