2015-07-10 54 views
0

我的SQL代碼是這樣的:MySQL存儲功能語法錯誤INNER JOIN表

DELIMITER $$ 

CREATE FUNCTION `get_theme`(`section_type` VARCHAR(50)) 
RETURNS VARCHAR(50) 
CONTAINS SQL 
SQL SECURITY DEFINER 

BEGIN 

DECLARE name,dirname VARCHAR(50) ; 
SELECT a.name INTO name , a.dirname INTO dirname FROM themes as a 
INNER JOIN sections as b ON 
a.section_id = b.id 
WHERE b.name = 'administrators' AND a.`status` = 'Y'; 

RETURN name + '#' + dirname; 
END $$ 

DELIMITER ; 

當我執行在heidiSQL 9.2.0.4961這個代碼,我得到這個錯誤:沒有定義的變量:一個

但當我執行此代碼:

SELECT a.name as name , a.dirname as dirname FROM themes as a 
INNER JOIN sections as b ON 
a.section_id = b.id 
WHERE b.name = 'administrators' AND a.`status` = 'Y'; 

有沒有問題,它工作得很好。

我的代碼有什麼問題?任何想法 ?

回答

1

您使用的語法錯誤。 正確的語法是:

SELECT list_of_expressions INTO list_of_variables FROM ... 


取代這一片段:

SELECT a.name INTO name , a.dirname INTO dirname FROM themes as a 

這一個:

SELECT a.name, a.dirname INTO name , dirname FROM themes as a 

一個備註:不分配給變量表中與列相同的名稱具有fo例如:DECLARE name,dirname,如果您想在查詢中稍後使用它,會導致混淆和錯誤。使用變量前綴,例如:DECLARE v_name,v_dirname