2012-08-11 44 views
1

我試圖用波斯日期 這些MySQL的功能我得到這個錯誤,但我想不出解決它日期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 

爲什麼這個錯誤發生?如何解決呢?

回答

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

+0

感謝您的幫助,它的工作。 – 2012-08-11 03:01:10

+0

但我已經返回並結束了該功能,因爲我提出了疑問。 – 2012-08-11 03:01:40