2016-11-14 55 views
0

我花了幾個小時,我不明白爲什麼這是突出顯示紅色,我不知道我的錯誤是什麼。MySQL存儲的函數語法錯誤

CREATE FUNCTION Student_Section_Count(StudentID INT) 

Returns INT 

Begin 

DECLARE section_Registred INT; 

SET section_Registred= (Select COUNT(DISTINCT Section.ID) as 'Students Registration Count' 

FROM Section 
Inner Join 
Registration on registration.StudentID=Section.ID 

Where registration.StudentID=StudentID); 

Return Section_Registred; 

END$$ 

Delimiter; 

它強調END和分隔符,以及INT從返回INT

回答

0

默認情況下,分號;是mysql中的分隔符。因此,當使用mysql客戶端時,傳遞給服務器的函數定義只能達到第一個分號,即DECLARE section_Registred INT;。其餘的函數定義不會傳遞給服務器。這會產生一個錯誤。要在函數定義中加入分號,您需要將您的分隔符從分號改爲其他。所以在定義函數之前寫下面的sql來改變分隔符:

DELIMITER $$

經過上面的sql爲你的函數寫入sql。在函數定義的最後添加新定界符$$END。現在,函數的整個定義被傳遞給服務器,直到新的分隔符。這將解決你的錯誤。在您的功能定義結束後,將分隔符更改爲默認值,如下所示:

DELIMITER ;

還請記住選擇一個不存在於您的函數定義內任何地方的分隔符。

0

嗯,我想我想通了我自己。

DELIMITER $$ 

CREATE FUNCTION Student_Section_Count(StudentID INT) 

Returns INT 

Begin 

DECLARE section_Registred INT; 

SET section_Registred= (Select COUNT(DISTINCT Section.ID) as 'Students Registration Count' 

FROM Section 

Inner Join 

Registration on registration.StudentID=Section.ID 

Where registration.StudentID=StudentID); 

Return Section_Registred; 

END$$