2013-08-27 80 views
0

我想在插入發生在我的表格之一時轉換字段。mysql觸發器更新無法正常工作

檢查& ged's =>檢查和geds。

DELIMITER $$ 
CREATE TRIGGER `perma_limk_insertion` AFTER INSERT 
ON `tbl_magazine_subscription` 
FOR EACH ROW BEGIN 

    DECLARE magazine_name VARCHAR(100); 
    DECLARE magazine_name_from_table VARCHAR(100); 
    DECLARE magazine_name_new VARCHAR(100); 
    SET @magazine_name_from_table := (SELECT MAGAZINE_NAME FROM tbl_magazine WHERE MAGAZINE_ID = NEW.magazine_id LIMIT 1); 
    SET @magazine_name   := REPLACE(@magazine_name_from_table,'&','and'); 
    SET @magazine_name_new  := REPLACE(@magazine_name,"'",''); 
    UPDATE tbl_magazine SET perma_link = @magazine_name_new WHERE MAGAZINE_ID = NEW.magazine_id; 
END$$ 
DELIMITER ; 

但使用上述查詢他們沒有更新。如果在我的查詢中有任何錯誤

如果有人知道這個請幫幫我。

+0

任何更新....?.. – beck03076

回答

0

試試這個,從任何地方刪除「@」。並使用INTO當你想要從表中選擇一個值到一個變量中。

DELIMITER $$ 
     CREATE TRIGGER perma_limk_insertion AFTER INSERT ON tbl_magazine_subscription FOR EACH ROW BEGIN 

      DECLARE magazine_name VARCHAR(100); 
      DECLARE magazine_name_from_table VARCHAR(100); 
      DECLARE magazine_name_new VARCHAR(100); 

    //changed from SET to INTO here 
    SELECT MAGAZINE_NAME INTO magazine_name_from_table FROM tbl_magazine 
    WHERE MAGAZINE_ID = NEW.magazine_id LIMIT 1; 

      SET magazine_name   := REPLACE(magazine_name_from_table,'&','and'); 
      SET magazine_name_new  := REPLACE(magazine_name,"'",''); 
      UPDATE tbl_magazine SET perma_link = magazine_name_new WHERE MAGAZINE_ID = NEW.magazine_id; 
     END$$ 

    DELIMITER ; 

要知道更多aboue會話變量和局部變量,請在這裏,

MySQL: @variable vs. variable. Whats the difference?