8

I'm working with Mysql 5.1.28-rc on freebsd. I've just decided to use stored procedures in MYSQL and created a test procedure as below:

DELIMITER $$ 
DROP PROCEDURE IF EXISTS test $$
CREATE PROCEDURE test( IN test VARCHAR(22) )
BEGIN
 DECLARE count INT(11);
 SET count = (SELECT COUNT(*) FROM Test WHERE test_column = test );
 SELECT count;
 IF count = 0 THEN
  SET count = 1;
 ELSE
  SET count = 2;
 ENDIF;
END $$
DELIMITER;

This procedure works well without IF statement in it , but with the if statement it gives, 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 '; END'

How can I solve this issue? Where is the problem?

3 Answers 3

20

ENDIF requires a space in MySQL doesn't it? i.e. END IF

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

2 Comments

holy cow , how could I miss that :)
It's nigh on impossible to spot typos in your own code, don't worry. Let's hear it for pair programming or, if you can't do that, SO... :D
5

Just need space in end if in the stored procedure

2 Comments

Can you justify your answer? E.g. with a link to the documentation?
Justification? mona just copied the answer above
0

Should not use variables like count or something. So please find the solution for this-

DELIMITER $$ 
DROP PROCEDURE IF EXISTS test $$
CREATE PROCEDURE test(IN test VARCHAR(22) )
BEGIN
 DECLARE var_count INT;
 SET var_count = (SELECT COUNT(*) FROM test WHERE test.textfield = test);
 IF var_count = 0 THEN
  SET var_count = 1;
 ELSE SET var_count = 2;
 END IF;
 SELECT var_count;
END $$

Comments

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.