Create Stored Procedure with Case Statement
<p>I am trying to create a stored procedure and I seem to be getting it wrong. I only use SQL when I need to, so please forgive a dev and help out. I want to include in the stored procedure a SQL query that does an inner join between two tables: user_students and student_grades_summary. Here is the stored procedure I wrote: </p>

<pre><code>CREATE PROCEDURE GradeStudents( in scoreover100 float, out s_grade char(3))
BEGIN 
CASE
        WHEN (student_grades_summary.aggregate &gt;= 80 &amp;&amp; student_grades_summary.aggregate &lt;= 100 &amp;&amp; student_grades_summary.level = 'JSS') THEN
            SET student_grades_summary.grade = 'A1';
        WHEN(student_grades_summary.aggregate &gt;= 70 AND student_grades_summary.aggregate &lt;= 79 AND student_grades_summary.level = 'JSS') THEN
            SET student_grades_summary.grade = 'B1';
        WHEN (student_grades_summary.aggregate &gt;= 60 AND student_grades_summary.aggregate &lt;= 69 AND student_grades_summary.level = 'JSS') THEN
            SET student_grades_summary.grade = 'B2';
        WHEN(student_grades_summary.aggregate &gt;= 55 AND student_grades_summary.aggregate &lt;= 59 AND student_grades_summary.level = 'JSS') THEN
            SET student_grades_summary.grade = 'P1';
        WHEN(student_grades_summary.aggregate &gt;= 50 AND student_grades_summary.aggregate &lt;= 54 AND student_grades_summary.level = 'JSS') THEN
            SET student_grades_summary.grade = 'P2';
        WHEN(student_grades_summary.aggregate &lt;= 49 AND student_grades_summary.level = 'JSS')
            SET student_grades_summary.grade = 'F';
        WHEN(student_grades_summary.aggregate &gt;= 85 AND student_grades_summary.aggregate &lt;= 100 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'A1';
        WHEN(student_grades_summary.aggregate &gt;= 80 AND student_grades_summary.aggregate &lt;= 84 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'B2';
        WHEN(student_grades_summary.aggregate &gt;= 75 AND student_grades_summary.aggregate &lt;= 79 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'B3';
        WHEN(student_grades_summary.aggregate &gt;= 70 AND student_grades_summary.aggregate &lt;= 74 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'C4';
        WHEN(student_grades_summary.aggregate &gt;= 65 AND student_grades_summary.aggregate &lt;= 69 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'C5';
        WHEN(student_grades_summary.aggregate &gt;= 60 AND student_grades_summary.aggregate &lt;= 64 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'C6';
        WHEN(student_grades_summary.aggregate &gt;= 55 AND student_grades_summary.aggregate &lt;= 59 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'D7';
        WHEN(student_grades_summary.aggregate &gt;= 50 AND student_grades_summary.aggregate &lt;= 54 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'E8';
        WHEN(student_grades_summary.aggregate &lt;= 49 AND student_grades_summary.level = 'SSS') THEN
            SET student_grades_summary.grade = 'F9';
END CASE;
</code></pre>

<p>And this is the SQL query that I am trying to include the stored procedure in. Basically I am trying to add values to the grade column in student_grades_summary by using the stored procedure. Anyways here is the sql:</p>

<pre><code>select user_students.fname as FirstName, user_students.lname as LastName
     , student_grades_summary.subject as CoreSubjects
     , student_grades_summary.aggregate as Scoreover100
     , student_grades_summary.subjectrank as Position
     , student_grades_summary.term as Term
     , student_grades_summary.level as Level
     , student_grades_summary.class as Class
     , student_grades_summary.section as Section
     , student_grades_summary.session as Session
FROM student_grades_summary 
JOIN user_students 
   ON student_grades_summary.level = user_students.level 
   AND student_grades_summary.class = user_students.class 
   AND student_grades_summary.section = user_students.section 
JOIN config_grades 
   ON student_grades_summary.level = config_grades.level 
WHERE user_students.level = 'JSS' 
   AND user_students.class = '2' 
   AND student_grades_summary.session = '2015/2016'
</code></pre>

<p>So in phpmyadmin, I get a lot of red squiggly lines saying there is an error in SQL. I have tried to look at the code and find what the error is but haven't been able to.</p>