2016-04-08 16 views
0

後調用觸發器時獲得更新的列。這是我的表結構如何從表更新

mysql> select * from product_table; 
+----------------+---------------------+ 
| product_number | product_description | 
+----------------+---------------------+ 
|    1 | product one   | 
|    2 | product two   | 
|    3 | product three  | 
|    9 | product five  | 
|    10 | product six   | 
|    11 | product six   | 
+----------------+---------------------+ 

我想說的更新一行說,從表像這樣

UPDATE product_table SET product_description ="product seven" WHERE product_number=9; 
9日產品數量

所以,在觸發我可以從表product_table

我寫了這樣的觸發器,它沒有任何錯誤得到了創建相應更新的產品數量。

DELIMITER // 
CREATE TRIGGER product_table_update 
     AFTER UPDATE 
     ON product_table 
     FOR EACH ROW 
BEGIN 


     DECLARE l_product_number INT; 
    set @l_table_name = 'product_table'; 
    set @l_action = 'updation'; 


       SET @l_table_column = 'product_description'; 
select new.product_number into @l_product_number from product_table;// here is i think where the problem is , i am trying to fetch the updated row to l_product_number 

     IF (OLD.product_description <> NEW.product_description) THEN 
     SET @oldval = OLD.product_description; 
     SET @newval = NEW.product_description; 
     select concat(@oldval,@newval) into @l_description; 

     END IF; 

     INSERT INTO audit_table_test 
     (table_name, 
     changed_row_id, 
     action, 
     table_column, 
     change_desciption, 
     change_time 
    ) 
     VALUES 
    (@l_table_name, 
     @l_product_number, 
     @l_action, 
     @l_table_column, 
     @l_description, 
    NOW() 
    ); 
END; // 
DELIMITER ; 

然後當我嘗試更新這樣

UPDATE product_table SET product_description ="product seven" WHERE product_number=11; 

此錯誤是顯示

ERROR 1172 (42000): Result consisted of more than one row 

我知道這個問題必須在此代碼

select new.product_number into @l_product_number from product_table;// here is i think where the problem is , i am trying to fetch the updated row to l_product_number 

請有人幫助我獲取更新排在調用此觸發

回答

1

嘗試:

... 
SET @l_table_column = 'product_description'; 

/* 
here is i think where the problem is , i am trying to fetch the 
updated row to l_product_number 
*/ 
-- select new.product_number into @l_product_number from product_table; 
SET @l_product_number := NEW.product_number; 

IF (OLD.product_description <> NEW.product_description) THEN 
... 
+0

thaaanQ非常多,現在我可以在很多地方應用此邏輯,U R A生命的救星!!!! – Mukund