我试图将这些mysql函数用于波斯日期 我有这个错误,但我无法解决它
SQL query:
CREATE FUNCTION `persian_day` (
indate date
) RETURNS int( 11 ) BEGIN declare j int;
MySQL said: Documentation
#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 3
我使用mysqlnd 5.0.8-dev和phpmyadmin(xampp包)
这是我的功能
CREATE FUNCTION `persian_mod`(a double,b double) RETURNS int(11)
return (a - b * floor(a / b))
CREATE FUNCTION `persian_div`(a double,b double) RETURNS int(11)
return (floor(a / b))
CREATE FUNCTION `persian_pj`(y int,m int,d int) RETURNS int(11)
return ( 227260 +
1029983 * persian_div((y - 474), 2820) +
365 * ( (persian_mod((y - 474), 2820) + 474) - 1) +
persian_div(682 * (persian_mod((y - 474), 2820) + 474) - 110, 2816) +
if(m > 6, 30 * m + 6, 31 * m)
+ d )
CREATE FUNCTION `persian_day`(indate date) RETURNS int(11)
begin
declare j int;
declare a int;
declare b int ;
declare c int;
declare d int;
declare yearr int;
declare f double;
declare monthh int;
set j=TO_DAYS(indate) ;
set a = j - persian_pj(475, 0, 1) ;
set b = persian_div(a, 1029983) ;
set c = persian_mod(a, 1029983) ;
set d = if(c <> 1029982, persian_div(2816 * c + 1031337, 1028522), 2820) ;
set yearr = 474 + 2820 * b + d;
set f = (1 + j) - persian_pj(yearr, 0, 1);
set monthh= if(f > 186, ceil((f - 6) / 30) - 1, ceil(f / 31) - 1);
return (j - (persian_pj(yearr, monthh, 1) - 1));
end
为什么会发生这种错误?以及如何解决?
答案 0 :(得分:2)
创建函数时的基本语法是你应该有一个return语句。
CREATE FUNCTION fnctionName (paramList)
RETURNS datatype
BEGIN
RETURN valueToReturn;
END
但是如果你在函数中有特殊的计算(当然是多行),你需要先更改分隔符:
DELIMITER $$
CREATE FUNCTION fnctionName (paramList)
RETURNS datatype
BEGIN
DECLARE ....;
..... other codes ;
RETURN valueToReturn;
END $$
DELIMITER ;
回到你的情况,
CREATE FUNCTION `persian_day` (indate date) RETURNS int( 11 )
BEGIN
declare j int;
您没有RETURN
值,并且您没有使用END
关键字关闭该功能。就像你在其他功能上所做的一样。
<强> CREATE FUNCTION Example 强>