0

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.

3
  • Note: there is no if in SQL. Quel SQL? Commented Dec 10, 2017 at 23:29
  • 2
    Your title says TSQL, you've tagged tsql and sql-server, but then mentioned that you're using MySQL in a comment below. Please edit your question to clarify what you're using. Commented Dec 11, 2017 at 0:24
  • 1
    you tagged the question as sqlserver, tsql which microsoft SQL server not MySql... Next time use MySQL tag Commented Dec 11, 2017 at 0:32

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for a fast reply, thou I'm afraid if I remove the "then" I get another error saying that it's missing. I'm using MySQL workbench, maybe that helps?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.