第一次寫(xiě)MySQL FUNCTION,一直報(bào)錯(cuò),
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
本來(lái)的函數(shù):
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
這段函數(shù)在Navicat上邊執(zhí)行不起來(lái),在多次嘗試之后將代碼修改為以下:
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 ;
問(wèn)題解決。
默認(rèn)情況下,delimiter是分號(hào);。
總結(jié)
以上就是本文關(guān)于navicat不能創(chuàng)建函數(shù)解決方法分享的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:簡(jiǎn)述Redis和MySQL的區(qū)別、MYSQL子查詢(xún)和嵌套查詢(xún)優(yōu)化實(shí)例解析、幾個(gè)比較重要的MySQL變量等,有什么問(wèn)題可以隨時(shí)留言,小編會(huì)及時(shí)回復(fù)大家的。感謝朋友們對(duì)本站的支持!
您可能感興趣的文章:- navicat中創(chuàng)建存儲(chǔ)過(guò)程、觸發(fā)器和使用游標(biāo)的簡(jiǎn)單實(shí)例(圖文)
- navicat 8 創(chuàng)建數(shù)據(jù)庫(kù)與創(chuàng)建用戶(hù)分配權(quán)限圖文方法
- 使用navicat 8實(shí)現(xiàn)創(chuàng)建數(shù)據(jù)庫(kù)和導(dǎo)入數(shù)據(jù) 管理用戶(hù)與權(quán)限[圖文方法]