创建MySQL函数时出现语法错误

时间:2013-04-08 09:35:09

标签: mysql sql function

CREATE FUNCTION count(name varchar(20))
        RETURNS integer
        BEGIN
        DECLARE d_count integer;
                select count(*) into d_count
                from ins
                where ins.name = name
        RETURN d_count;
        END

上面的SQL代码显示存在语法错误。

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 'RETURN (d_count); END' at line 8

任何想法如何解决?

2 个答案:

答案 0 :(得分:2)

语法错误,在SELECT语句后错过; -

CREATE FUNCTION count (name varchar(20))
RETURNS integer
BEGIN
  DECLARE d_count integer;
  SELECT COUNT(*) INTO d_count FROM ins WHERE ins.name = NAME;
  RETURN d_count;
END

答案 1 :(得分:0)

试试这个

CREATE FUNCTION count(@name varchar(20))
        RETURNS integer
        BEGIN

        RETURN( select count(*) 
                from ins
                where ins.name = @name);

        END