2015-11-26 55 views
1

這是我的功能如何解決此錯誤#1064。同時創造功能

CREATE FUNCTION CAP_FIRST (input VARCHAR(255)) 

RETURNS VARCHAR(255) 

DETERMINISTIC 

BEGIN 
    DECLARE len INT; 
    DECLARE i INT; 

    SET len = CHAR_LENGTH(input); 
    SET input = LOWER(input); 
    SET i = 0; 

    WHILE (i < len) DO 
     IF (MID(input,i,1) = ' ' OR i = 0) THEN 
      IF (i < len) THEN 
       SET input = CONCAT(
        LEFT(input,i), 
        UPPER(MID(input,i + 1,1)), 
        RIGHT(input,len - i - 1) 
       ); 
      END IF; 
     END IF; 
     SET i = i + 1; 
    END WHILE; 

    RETURN input; 
END; 

我使用這個函數來獲取每個單詞的第一個字母大寫,但它給我的錯誤。我搜索解決方案,我發現了一些解決方案還但他們不工作。

MySQL的說:文檔

#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 8 

回答

0

嘿,我找到了解決這個我新這是正常工作的代碼。

DELIMITER $$ 
CREATE FUNCTION `db_name`.`Ucfirst`(`word` VARCHAR(255)) 
    RETURNS VARCHAR(255) 
    BEGIN 
    DECLARE len INT; 
    DECLARE i INT; 

    SET len = CHAR_LENGTH(word); 
    SET word = LOWER(word); 
    SET i = 0; 

    WHILE (i < len) DO 
     IF (MID(word,i,1) = ' ' OR i = 0) THEN 
      IF (i < len) THEN 
       SET word = CONCAT(
        LEFT(word,i), 
        UPPER(MID(word,i + 1,1)), 
        RIGHT(word,len - i - 1) 
       ); 
      END IF; 
     END IF; 
     SET i = i + 1; 
    END WHILE; 

    RETURN word; 
    END$$ 

DELIMITER ; 
0

分號缺失:

SET i = i + 1; 
END WHILE; 

RETURN input;