WeirdGuy
WeirdGuy

Reputation: 13

TSQL Syntax error: missing 'if'

I'm making a procedure for a school project that removes a specific course from the course table if it's not in use.

According to everything I've read this is the correct setup but tere's an error on that last "END;" line that says there's a "missing 'if'"

Delimiter //

CREATE PROCEDURE DeleteCourse
    (
    pCourseNumber varchar (7)
    )
BEGIN

    if NOT EXISTS(SELECT * FROM trackcourses WHERE courseNumber = pCourseNumber)
        then
        BEGIN

            DELETE 
            FROM courses
            WHERE courseNumber = pCourseNumber;

            DELETE
            FROM restrictors
            WHERE courseNumber = pCourseNumber;

            select row_count();
        END;    
    else
    BEGIN

    return 'Course could not be deleted';

    END;

END; //  <-- Syntax error: missing 'if' 

Any thoughts and ideas are greatly appreciated!

Thanks,

Weird.

Upvotes: 0

Views: 987

Answers (1)

Grantly
Grantly

Reputation: 2556

You may need to use the word THEN on the same line as the IF, and use END IF instead of BEGIN END blocks.

IF <statement> THEN
...
END IF

See here for MySQL syntax

If you just use:

...
IF NOT EXISTS(SELECT * FROM trackcourses WHERE courseNumber = pCourseNumber) THEN
    DELETE 
    FROM courses
    WHERE courseNumber = pCourseNumber;

    DELETE
    FROM restrictors
    WHERE courseNumber = pCourseNumber;

    select row_count();
ELSE
   RETURN 'Course could not be deleted';
END IF
...

It should be fine

Upvotes: 1

Related Questions