2010-09-14 50 views
1

我是數據庫開發新手。請幫助我創建一個將數據從一個表移動到另一個表的觸發器。使用更新觸發器將數據從一個表移動到另一個表

我有兩個表,其中一個包含「事務狀態」,我想將事務狀態更改的記錄移動到另一個已完成事務的表中。因此,一個表中的值將被刪除,並會插入到另一個表中。

請指正在以下觸發:

create trigger transaction_state after update on test_archive for each row begin 
insert into test_me(select * from test_archive where new.Transaction_status = 2); 
delete from test_archive where new.Transaction_status = 2; 
end; 

回答

1

爲什麼我覺得我幫你做作業?寫入的觸發器可能會在有人將行更新爲Transaction_Status = 2時移動所有行。由於您沒有將new表加入test_archive表,因此您的WHERE子句對於所有行都將爲true。

如果您真的想將Transaction_status = 2的所有行從test_archive移動到test_me,那麼請刪除FOR EACH和對NEW表的引用。

create trigger transaction_state after update on test_archive 
    begin 
    insert into test_me 
     select * from test_archive where Transaction_status = 2; 
    delete from test_archive where Transaction_status = 2; 
    end; 
+0

4年後爲我節省了很多時間:) ..謝謝! – DntFrgtDSemiCln 2015-06-09 17:00:53

相關問題