0
create procedure proc_Grade(in roll int(5),in name varchar(20),in marks int(5))
-> begin
-> declare grade varchar(20);
-> insert into student values(roll,name,marks);
-> if marks<=100 and marks>=67 then set grade="distinction";
-> if marks<=66 and marks>=57 then set grade="firstclass";
-> if marks<=56 and marks>=47 then set grade="secdclass";
-> if marks<=46 and marks>=40 then set grade="pass";
-> else set grade="fail";
-> insert into result values(roll,grade);
-> end if;
-> end$$

ERROR 1064 (42000): 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 '' at line 12

Here's a screenshot of command line client

6
  • 1
    It sounds like you forgot delimiter $$ at the first line Commented Oct 27, 2018 at 5:40
  • Even after doing that I'm getting the same error Commented Oct 27, 2018 at 5:48
  • Are you sure you want to add else set grade="fail"? it will set "fail" on more records then you expect. Commented Oct 27, 2018 at 6:05
  • Yes it's required Commented Oct 27, 2018 at 6:12
  • @kunaldeshpande: you have to execute delimiter $$ before create procedure statement? can you post screenshot of your mysql client with error stack displayed. Commented Oct 27, 2018 at 6:25

1 Answer 1

1

As error messages go, that one is particularly unhelpful.

There are two remaining issues:

  1. The position of the second insert statement (result) which will only run for students who 'fail' the course.

  2. The IF statement syntax where the second, and subsequent, IF should be ELSEIF or you should terminate each IF statement with END IF;

The syntax should be in the form

IF condition1 THEN
  statement1;
ELSEIF condition2 THEN
  statement2;
END IF;  

The documentation is at https://dev.mysql.com/doc/refman/5.7/en/if.html (though watch out for the typo which misses the semi-colon after END IF;)

Your procedure would look like this:

delimiter $$

create procedure proc_Grade(in roll int(5),in name varchar(20),in marks int(5))
begin
  declare grade varchar(20);

  insert into student values(roll,name,marks);

  if marks<=100 and marks>=67 then 
    set grade="distinction";
  elseif marks<=66 and marks>=57 then
    set grade="firstclass";
  elseif marks<=56 and marks>=47 then
    set grade="secdclass";
  elseif marks<=46 and marks>=40 then
    set grade="pass";
  else
    set grade="fail";
  end if;

  insert into result values(roll,grade);

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

2 Comments

The error message is a MySQL quirk but still helpful, if you know what it means: unexpected thing found next in the parse buffer: ...near 'unexpected thing' at line.... An empty string in the error means there is nothing left to parse, i.e. the parser ran out of tokens in a state when it simply can't be possible that your statement is complete. Here, the parser expected the last END to be followed by IF, not nothing. (The delimiter is already not in the parse buffer because the CLI actually strips it.) SELECT * FROM; or SELECT * FROM table1 WHERE; would generate the same error.
@Michael-sqlbot, thanks for that contribution, a very clear explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.