doumalu9257
2016-04-02 15:34
浏览 86
已采纳

运行代码时MySQL函数中的语法错误

I am getting this error:

#1064 - 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 4

when i run followed code in phpmyadmin also appearing this error beside of second declare expression:

unrecognized statement type (near declare)

What can be cause of these error? (Phpmyadmin, version, etc)

create function fnc_generate_url(title_in varchar(250))
    returns varchar(250)
        begin
            declare v_count int; 
            declare v_return varchar(250);
            declare cr_count cursor
            for
                select count(1) from tbl_page where page_title like concat('%',title_in,'%');

            open cr_count;
                fetch cr_count into v_count;
            close cr_count;

            if v_count = 0 then
                set v_return = replace(trim(title_in), ' ', '-');
            else
                set v_return =  concat(replace(trim(title_in), ' ', '-'),'-',v_count);
            end if;

            return v_return;
        end;

图片转代码服务由CSDN问答提供 功能建议

我收到此错误:

#1064 - 您的SQL语法有错误; 查看与您的MySQL服务器版本对应的手册,以便在第4行附近使用正确的语法

当我运行时,跟随phpmyadmin中的代码也出现此错误 第二个声明表达式旁边:

无法识别的语句类型(接近声明)

可能是什么原因 这些错误? (Phpmyadmin,版本等)

  create function fnc_generate_url(title_in varchar(250))
返回varchar(250)
 begin 
 declare v_count int;  
声明v_return varchar(250); 
声明cr_count cursor 
 for 
从tbl_page中选择count(1),其中page_title类似于concat('%',title_in,'%'); 
 
打开cr_count; \  n将cr_count提取到v_count; 
关闭cr_count; 
 
如果v_count = 0则设置v_return = replace(trim(title_in),'',' - '); 
 else 
 set v_return = concat(  replace(trim(title_in),'',' - '),' - ',v_count); 
 end if; 
 
 return v_return; 
 end; 
   
  
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanaixuan7385 2016-04-02 16:19
    已采纳

    Works fine if we use delimiters.

    Try this:

    DELIMITER $$
    create function fnc_generate_url(title_in varchar(250))
        returns varchar(250)
            begin
                declare v_count int; 
                declare v_return varchar(250);
                declare cr_count cursor
                for
                    select count(1) from tbl_page where page_title like concat('%',title_in,'%');
    
                open cr_count;
                    fetch cr_count into v_count;
                close cr_count;
    
                if v_count = 0 then
                    set v_return = replace(trim(title_in), ' ', '-');
                else
                    set v_return =  concat(replace(trim(title_in), ' ', '-'),'-',v_count);
                end if;
    
                return v_return;
            end;
    $$
    DELIMITER ;
    
    点赞 打赏 评论

相关推荐 更多相似问题