2

I'm following some tutorials on mysql function creation but I keep getting the following error.

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 5:

 CREATE FUNCTION getstatisticscount (h VARCHAR(35),d date)
  RETURNS INT
  DETERMINISTIC
   BEGIN
    DECLARE tel INT;
    SELECT count(hash) into tel from statsitieken where hash=h and lastvisit between concat(d,' 00:00:00') and concat(d,' 23:59:59') group by hash;  
    RETURN tel;
   END;

I can for my life not find where line 5 is, but no matter which line I put it on, I keep getting this error. If I remove this function from the sql fiddle code it's all fine. I can't find what's wrong with it... except maybe flawed tutorials.

http://sqlfiddle.com/#!2/70f0a

4
  • 3
    Nice! I didn't know about that before - sqlfiddle.com Commented Nov 1, 2012 at 8:37
  • I've added the sql fiddle link Commented Nov 1, 2012 at 8:39
  • Is it still not working for you? Where are you trying to execute these statements, in the console, in an application, or in a web or desktop based MySQL client? Commented Nov 1, 2012 at 9:26
  • Mysql version throws an error in the end that must be solved by my host. Column count of mysql.proc is wrong. Expected 20, found 16. The table is probably corrupted; Commented Nov 1, 2012 at 10:04

1 Answer 1

3

Use Delimiter

delimiter //
CREATE FUNCTION getstatistics(h VARCHAR(35),d date)
  RETURNS INT
  DETERMINISTIC
   BEGIN
    DECLARE tel INT;
    SELECT count(hash) INTO tel
    FROM statistics
    WHERE
      hash=h
      AND lastvisit BETWEEN concat(d,' 00:00:00') AND concat(d,' 23:59:59')
    GROUP BY hash;  
    RETURN tel;
   END
//

DELIMITER ;

For more info: http://dev.mysql.com/doc/refman/5.1/en/stored-programs-defining.html

Edit: basically since your stored procedure separates out statements with semicolons (;) and the method that defines your procedure ALSO uses semicolons to separate out statements, it's hard to impossible for MySQL to figure out where your procedure begins and ends. I also edited the SQL statement above to return the delimiter back to the default semicolon.

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

3 Comments

I'd wish it would work but it returns 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 'delimeter // CREATE FUNCTION getstatisticscount (h VARCHAR(35),d date) RETURNS' at line 1:
It worked in HeidiSQL and MySQL command line. In SQL fiddle, try changing the query delimiter (the fourth button below the DDL box).
Can you try the suggestion in your app and not SQL fiddle: stackoverflow.com/questions/12166380/…

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.