2011-09-22 46 views
1

我想要做一個互動的存儲過程 我的意思是,在執行後用戶應輸入一個字「COUNT(*)」附近的語法錯誤?

我寫了這一點,但它不工作..

DELIMITER $$ 

DROP PROCEDURE IF EXISTS ric_forn$$ 
CREATE PROCEDURE ric_forn (IN nome_forn VARCHAR(100) , OUT msg VARCHAR(100)) 
BEGIN 
DECLARE num_rec INT; 

IF (nome_forn = '') THEN 
    SET msg = "Attenzione il nome inserito non è valido !"; 
END IF; 

SELECT COUNT (*) INTO num_rec FROM Fornitori WHERE Des_Fornitore = nome_forn; 

IF num_rec = 0 THEN 
    SET msg = "Nessun record trovato !"; 

ELSE 

SELECT Id_Fornitore,Des_Fornitore,Ind_Fornitore WHERE Des_Fornitore = nome_forn; 
    SET msg = "Records trovati:"; 

END IF; 

END$$ 

DELIMITER ; 

我得到這個錯誤:

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 '*) INTO num_rec FROM Fornitori WHERE Des_Fornitore = nome_forn;

+0

嗯,確實'SELECT COUNT(*)INTO num_rec FROM Fornitori WHERE Des_Fornitore = nome_forn;'從CLI工作? – 2011-09-22 21:42:42

+0

我是一個初學者..你是什麼意思的CLI?...謝謝 – Roberto

+0

好的抱歉,你的意思是從外殼..即使我註釋掉這一行東西不順利無論如何.. – Roberto

回答

9

的空間和(*)之間COUNT是顯著。你應該把它們放在一起,不要留下空間。

SELECT COUNT(*) INTO ... 

例外是如果你SET SQL_MODE='IGNORE_SPACE'。閱讀Function Name Parsing and Resolution瞭解更多詳情。


你其他錯誤是,你忘了FROM子句中的查詢之一:

SELECT Id_Fornitore,Des_Fornitore,Ind_Fornitore WHERE Des_Fornitore = nome_forn; 

應該是:

SELECT Id_Fornitore,Des_Fornitore,Ind_Fornitore FROM Fornitori 
WHERE Des_Fornitore = nome_forn; 
+0

我刪除了你所說的空間,,現在我得到了一個不同的錯誤,指向 - > WHERE Des_Fornitore = nome_forn; SET msg =「記錄trovati:」; 謝謝 – Roberto