2012-12-01 129 views
0

我是新來的sql和我的大部分培訓是在MSSQL,但我正在MYSQL的課。我有一個存儲過程給我一個問題。我將分隔符設置爲//。存儲過程問題

CREATE PROCEDURE AddComment 
(
    name varchar(50), 
    emailAddress varchar(50), 
    Comments text 
) 
BEGIN 
DECLARE ComName varchar(50); 
DECLARE ComID int; 


SELECT name into ComName; 

SELECT ID into ComID from Commenters WHERE names = ComName; 

if ComID = NULL 
    BEGIN 
     INSERT INTO COMMENTERS ('names', 'emailAddresses') values (name, emailAddress); 
     SELECT ID into ComID from Commenters WHERE names = ComName; 
    END 

    INSERT INTO COMMENTS ('commentersID', 'Comments') values (ComID, Comments); 

END; 
// 

編輯

這是我現在有:

CREATE PROCEDURE AddComment 
(
    Username varchar(50), 
    UseremailAddress varchar(50), 
    UserComment text 
) 
BEGIN 
DECLARE ComName varchar(50); 
DECLARE ComID int; 


SELECT Username into ComName; 

SELECT ID into ComID from Commenters WHERE names = ComName; 

if ComID = NULL 
    BEGIN 
     INSERT INTO COMMENTERS(names,emailAddresses) values (Username, UseremailAddress); 
     SELECT ID into ComID from Commenters WHERE names = ComName; 
    END 

    INSERT INTO COMMENTS(commentersID, Comments) values (ComID, UserComment); 


END; 
// 

而且我得到的錯誤是:

ERROR 1064(42000):你有一個錯誤的SQL語法;檢查對應於你的MySQL服務器版本使用附近的正確語法手冊「BEGIN INSERT INT Ø評議人(姓名,emailAddresses)值(用戶名,USEREMAIL」在行1個

什麼想法?

+0

那是什麼你得到 – GreyBeardedGeek

+0

我通過學校從命令行運行錯誤:?這是錯誤正在打印出來。你的SQL語法有錯誤;檢查對應於您的MySQL服務器版本的手冊,以找到正確的語法以在'BEGIN INSE RT INTO COMMENTERS('names','emailAddresses')值(第2行的名稱,emailAddres)附近使用 –

+0

嘗試將引號從列名,即INSERT INTO COMMENTERS(名稱,emailAddresses)值(名稱,emailAddresses);爲了避免混淆,您可能想要爲列名命名參數,例如'name'參數爲'p_name'.. – GreyBeardedGeek

回答

0

你在你插入語句顛倒編碼列名和值

應該

INSERT INTO COMMENTERS (name, emailAddress) values ('names', 'emailAddresses') ; 
+0

我之前假設列已經超過了參數。 IE ... INSERT INTO _tablename_(_columns_)values(_parameter_);那是錯的嗎? –

+0

@ColeMietzner不,你是對的,但那不是你編碼的東西。列名不應該**有引號,值**應該有引號(或者是proc的變量) – Bohemian