mercredi 31 mai 2017

MySQL - IF/THEN statement determines INSERT INTO

I'm trying to set up an If/Then statement that determines what is inserted into the "course" table in my database. The first INSERT INTO limits the insert to 6 rows, and the second limits it to 7.

Here is what the code looks like:

If @courseprogram = 'PharmTech' Then
    INSERT INTO course (fameid, course_id, enroll_status)
    SELECT @fameid fame_id, course_id, 'S' enroll_status FROM calendar
    WHERE startdate >= @startdate AND program = @courseprogram ORDER BY startdate LIMIT 6;
Elseif @fullprogram = 'Medical Assisting'
    INSERT INTO course (fameid, course_id, enroll_status)
    SELECT @fameid fame_id, course_id, 'S' enroll_status FROM calendar
    WHERE startdate >= @startdate AND program = @courseprogram ORDER BY startdate LIMIT 7;
End if

When I try running this, I get the following error message:

Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'If @courseprogram = 'PharmTech' Then INSERT INTO course (fameid, course_id, enr' at line 1

Does anyone know what might be the issue here?

Also, I've read in a couple of posts around this issue that IF/THEN statements should be avoided in MySQL all together. Could anyone explain why this is and how else to approach the problem?

Aucun commentaire:

Enregistrer un commentaire