2013-07-01 85 views
0

我只是想使用觸發器而不是檢查約束和代碼之一,但它給了我一個錯誤。語法錯誤在mysql'觸發器'

CREATE TRIGGER conflict 
ON roozane 
FOR EACH ROW 
BEGIN 
if rDate = NEW.rDate then 
if NEW.rStartTime < rStartTime AND NEW.rEndTime < rEndTime then 
INSERT INTO roozane (rID,rDate,rStartTime,rEndTime,rPlace,rComment,rType) values (NEW.rID,NEW.rDate,NEW.rStartTime,NEW.rEndTime,NEW.rPlace,NEW.rComment,NEW.rType); 
end if 
end if 
END;$$ 

和錯誤

#1064 - 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 'ON roozane FOR EACH ROW BEGIN if (rDate=NEW.rDate) then if (NEW.rStart' at line 2 

編輯

CREATE TRIGGER conflict BEFORE INSERT 
ON roozane 
FOR EACH ROW 
BEGIN 
if rDate = NEW.rDate then 
if NEW.rStartTime < rStartTime AND NEW.rEndTime < rEndTime then 
INSERT INTO roozane (rID,rDate,rStartTime,rEndTime,rPlace,rComment,rType) values (NEW.rID,NEW.rDate,NEW.rStartTime,NEW.rEndTime,NEW.rPlace,NEW.rComment,NEW.rType); 
end if 
end if 
END;$$ 

和錯誤

#1064 - 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 '' at line 7 

TNX求助

+0

您的* trigger_time *和* trigger_event *在哪裏? 'CREATE TRIGGER衝突 BEFORE UPDATE ON roozane' http://dev.mysql.com/doc/refman/5.5/en/create-trigger.html – DevlshOne

+0

啊固定TNX但現在它轉發給INSERT命令和這裏顯示語法錯誤 – user2540401

+0

對不起,但我不明白你最後的評論。你能否發佈你的更新後的查詢和錯誤? – DevlshOne

回答

1

您需要* trigger_time *和* trigger_event *。例如: CREATE TRIGGER conflict AFTER INSERT

0

在每個end if之後需要用分號來終止這些複合語句。

在最後的END後面不需要分號,因爲大概您已經使用DELIMITER $$來更改mysql客戶端中的語句終止符。

我測試了以下內容。它沒有得到語法錯誤,但當然我沒有名爲roozane的表,所以我得到了一個不同的錯誤。 :-)

CREATE TRIGGER conflict BEFORE INSERT 
ON roozane 
FOR EACH ROW 
BEGIN 
if rDate = NEW.rDate then 
if NEW.rStartTime < rStartTime AND NEW.rEndTime < rEndTime then 
INSERT INTO roozane (rID,rDate,rStartTime,rEndTime,rPlace,rComment,rType) values (NEW.rID,NEW.rDate,NEW.rStartTime,NEW.rEndTime,NEW.rPlace,NEW.rComment,NEW.rType); 
end if; 
end if; 
END$$ 
0

你的觸發器有幾個問題。

至於語法和邏輯錯誤去

  1. 望着錯誤消息,顯然你沒有在你的腳本beggining使用DELIMITER $$
  2. 您的觸發器中有三個未聲明的變量rDaterStartTime,rEndTime。如果使用存儲過程級別變量,則需要首先聲明它們並最終爲其分配值。
  3. 作爲@BillKarwin在他的回答中提到,你必須有在每個IF ... END IF;語句的結束分號和您的觸發器的BEGIN...END塊的關閉END因爲你應該已經DELIMITER早些時候改爲$$後不需要分號。

話雖這麼說,你的觸發語法正確的版本可能會以下

DELIMITER $$ 
CREATE TRIGGER conflict 
BEFORE INSERT ON roozane 
FOR EACH ROW 
BEGIN 
    DECLARE rDate  DATE; 
    DECLARE rStartTime TIME; 
    DECLARE rEndTime TIME; 

    IF rDate = NEW.rDate THEN 
    IF NEW.rStartTime < rStartTime AND NEW.rEndTime < rEndTime THEN 
     INSERT INTO roozane (rID,rDate,rStartTime,rEndTime,rPlace,rComment,rType) 
     VALUES(NEW.rID,NEW.rDate,NEW.rStartTime,NEW.rEndTime,NEW.rPlace,NEW.rComment,NEW.rType); 
    END IF; 
    END IF; 
END$$ 

這裏是SQLFiddle演示顯示,現在您的觸發器被成功創建但什麼都不做因爲通過聲明的變量默認值爲NULL,其他值尚未分配給它們。

這裏去最重要的部分:事件是否與變量的問題將很遺憾,您的觸發不會因爲MySQL憑藉其觸發器不允許數據操作語句相當有限的支持(工作反正INSERT在你的情況)在同一張桌子上(你的情況下爲roozane)你正在附上你的觸發器。

現在,爲了幫助您解決觸發問題,您需要說明您希望觸發器檢查的內容。