navicat不能创建函数解决方法分享

时间:2022-05-04 02:51:24

第一次写MySQL FUNCTION,一直报错,

Err] 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 '`company_id` int) RETURNS varchar(20) CHARSET utf8

BEGIN

本来的函数:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
CREATE DEFINER=`33323`@`%` FUNCTION `createSaleCode`(`benginStr` varchar,`company_id` int) RETURNS varchar(20) CHARSET utf8
BEGIN
  DECLARE nearnum VARCHAR(20); 
 DECLARE nowdatepre VARCHAR(20);
 DECLARE numout VARCHAR(20); 
 SELECT a.sale_code INTO nearnum FROM d_sale a WHERE a.company_id = company_id ORDER BY a.sale_id DESC limit 1;
 SELECT concat(extract(year_month from now()),LPAD(extract(day from now()), 2, 0)) INTO nowdatepre;
 IF locate(nowdatepre,nearnum)>0 
  THEN
    set numout = nearnum +1;  
  ELSE
    set numout = concat(beginStr,nowdatepre,'00001'); 
  END IF;
 RETURN numout;
END

这段函数在Navicat上边执行不起来,在多次尝试之后将代码修改为以下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
delimiter $$
CREATE DEFINER=`12212`@`%` FUNCTION createSaleCode(benginStr varchar(20),company_id int(11) ) RETURNS varchar(20) CHARSET utf8
BEGIN
  DECLARE nearnum VARCHAR(20); 
 DECLARE nowdatepre VARCHAR(20);
 DECLARE numout VARCHAR(20); 
 SELECT a.sale_code INTO nearnum FROM d_sale a WHERE a.company_id = company_id ORDER BY a.sale_id DESC limit 1;
 SELECT concat(extract(year_month from now()),LPAD(extract(day from now()), 2, 0)) INTO nowdatepre;
 IF locate(nowdatepre,nearnum)>0 
  THEN
    set numout = nearnum +1;  
  ELSE
    set numout = concat(beginStr,nowdatepre,'00001'); 
  END IF;
 RETURN numout;
END$$
delimiter ;

问题解决。

默认情况下,delimiter是分号;。

总结

以上就是本文关于navicat不能创建函数解决方法分享的全部内容,希望对大家有所帮助。有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!

原文链接:http://blog.csdn.net/wojiaohuangyu/article/details/54629414