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

(编辑:jimmy 日期: 2024/11/13 浏览:2)

第一次写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

本来的函数:

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上边执行不起来,在多次尝试之后将代码修改为以下:

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不能创建函数解决方法分享的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:简述Redis和MySQL的区别、MYSQL子查询和嵌套查询优化实例解析、几个比较重要的MySQL变量等,有什么问题可以随时留言,小编会及时回复大家的。感谢朋友们对本站的支持!