您的位置:首页 > 其它

navicat不能创建函数

2017-01-20 10:45 435 查看
第一次写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是分号;。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: