2013-03-05 47 views
0

我想打一個2-3觸發1臺。例如(這是我的查詢):創建多個觸發器爲一個表

DELIMITER $$ 

CREATE OR REPLACE TRIGGER IF EXISTS insert_before_update_slugrapat BEFORE UPDATE ON slugrapat 
FOR EACH ROW 
BEGIN 
INSERT INTO record_pemakaian_slug_rapat(rc_sr_size, rc_sr_stockawal, rc_sr_holling, rc_sr_store, rc_sr_sales, rc_sr_sisa, rc_sr_lastdate) 
VALUES(new.sr_size, new.sr_stockawal, new.sr_holling, new.sr_store, new.sr_sales, new.sr_sisa, new.sr_lastdate); 
/
CREATE OR REPLACE TRIGGER IF EXISTS insert_before_update_slugrapat BEFORE UPDATE ON slugrapat 
FOR EACH ROW 
BEGIN 
INSERT INTO so_slugrapat(sr_size, sr_sisa, sr_stockakhir, sr_stockopname, sr_lastdate) 
VALUES(new.sr_size, new.sr_sisa, new.sr_stockakhir, new.sr_stockopname, new.sr_lastdate); 
END $$ 

DELIMITER ; 

和錯誤說:

Error code 1064, SQL state 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 'TRIGGER IF EXISTS insert_before_update_slugrapat BEFORE UPDATE ON slugrapat 
FOR ' at line 1 

我想已經張貼在計算器但它仍然無法正常工作許多方面。

+0

爲什麼存根'/'那裏? – hjpotter92 2013-03-05 08:06:22

+0

爲什麼不把所有內容合併爲一個,因爲它們出現在同一張表'BEFORE UPDATE'上? – SparKot 2013-03-05 08:13:36

+0

錯誤..因爲我在另一篇文章中讀到,它說在同一個腳本上的兩個觸發器,我必須添加「/」來代替mysql中的「GO」。這是錯的嗎? – ShenWu 2013-03-05 08:13:44

回答

0

檢查這個代替:

DROP TRIGGER insert_before_update_slugrapat; 

DELIMITER $$ 

CREATE TRIGGER insert_before_update_slugrapat 
    BEFORE UPDATE 
    ON slugrapat 
    FOR EACH ROW 
BEGIN 
    INSERT INTO record_pemakaian_slug_rapat(rc_sr_size, 
              rc_sr_stockawal, 
              rc_sr_holling, 
              rc_sr_store, 
              rc_sr_sales, 
              rc_sr_sisa, 
              rc_sr_lastdate) 
    VALUES (new.sr_size, 
      new.sr_stockawal, 
      new.sr_holling, 
      new.sr_store, 
      new.sr_sales, 
      new.sr_sisa, 
      new.sr_lastdate); 

    INSERT INTO so_slugrapat(sr_size, 
          sr_sisa, 
          sr_stockakhir, 
          sr_stockopname, 
          sr_lastdate) 
    VALUES (new.sr_size, 
      new.sr_sisa, 
      new.sr_stockakhir, 
      new.sr_stockopname, 
      new.sr_lastdate); 
END$$ 
DELIMITER; 
+0

哇!它完全工作..非常感謝你的火花! – ShenWu 2013-03-05 08:39:55

0

試用兩種解決方案。 1.Put所有的代碼裏面一個觸發器和使用多個開始和結束的聲明 2.取出分隔符,開始和結束

+0

仍然沒有工作..即時通訊困惑,因爲錯誤說:錯誤代碼1064,SQL狀態42000:你的SQL語法錯誤;檢查對應於你的MySQL服務器版本「insert_before_update_slugrapat BEFORE UPDATE ON slugrapat FOR EACH ROW INSERT我用近1行 – ShenWu 2013-03-05 08:22:54

+0

首先放棄你現有的觸發正確的語法手冊。 而在MySQL是否存在,如果它確實是失去觸發本身.. – divyabharathi 2013-03-05 08:38:41

+0

的定義,我已經找到了答案,從sparkot ..感謝您的幫助divy你不能更換觸發! – ShenWu 2013-03-05 08:48:29

0
DELIMITER $$ 

DROP TRIGGER IF EXISTS insert_before_update_slugrapat; 

CREATE TRIGGER insert_before_update_slugrapat BEFORE UPDATE ON slugrapat 
FOR EACH ROW 
BEGIN 
INSERT INTO record_pemakaian_slug_rapat(rc_sr_size, rc_sr_stockawal, rc_sr_holling, rc_sr_store, rc_sr_sales, rc_sr_sisa, rc_sr_lastdate) 
VALUES(new.sr_size, new.sr_stockawal, new.sr_holling, new.sr_store, new.sr_sales, new.sr_sisa, new.sr_lastdate); 
END $$ 

DROP TRIGGER IF EXISTS insert_before_update_slugrapat_1; 
CREATE TRIGGER insert_before_update_slugrapat_1 BEFORE UPDATE ON slugrapat 
FOR EACH ROW 
BEGIN 
INSERT INTO so_slugrapat(sr_size, sr_sisa, sr_stockakhir, sr_stockopname, sr_lastdate) 
VALUES(new.sr_size, new.sr_sisa, new.sr_stockakhir, new.sr_stockopname, new.sr_lastdate); 
END $$ 

DELIMITER ; 
+0

我已經找到了sparkot的答案..謝謝你的幫助Fathah! – ShenWu 2013-03-05 08:50:31

相關問題