2012-03-14 65 views
0

這是存儲過程,我試圖用MySQL你如何做一個變量的準備語句?

DELIMITER ## 
CREATE PROCEDURE exportFile() 
BEGIN 

DECLARE filename VARCHAR(255); 

SET filename = CONCAT('~/Sample',NOW(),'.csv')); 
SET @outfilestmt = concat('SELECT * INTO OUTFILE ',"'", filename,"'",' FROM Results') ; 

PREPARE stmt FROM @outfilestmt; 
EXECUTE stmt; 
DEALLOCATE PREPARE stmt; 

END ## 
DELIMITER ; 

這是錯誤我得到

ERROR 1064 (42000): 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 '); 
SET @outfilestmt = concat('SELECT * INTO OUTFILE ',"'", filename,"'",' FROM R' at line 6 

所需的結果:

call exportFile 
--~/Sample2012-03-14-10:42:51.cvs 

call exportFile 
--~/Sample2012-03-14-10:42:52.cvs 

call exportFile 
--~/Sample2012-03-14-10:42:53.cvs 

回答

1

一分號後失蹤

SET filename = CONCAT('~/Sample',NOW(),'.csv')) 

和一個支架太多(也由Devart注意到。謝謝)。更改爲

SET filename = CONCAT('~/Sample',NOW(),'.csv'); 

經過測試確定。作品!

+0

AT tombom我添加了冒號並更新了上面的錯誤信息 – stackoverflow 2012-03-14 15:00:39

+1

不要忘記刪除最後一個括號 - ')' – Devart 2012-03-14 15:01:55

+1

現在想補充一下。謝謝Devart。 @ Gah_Jamn-它看到我編輯的答案 – fancyPants 2012-03-14 15:02:50