Oracle 触发器在插入或更新时更新字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1833691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:34:21  来源:igfitidea点击:

Oracle Trigger Updating a field on an Insert or an Update

oracletriggers

提问by AlteredConcept

For some reason I'm running a blank on how to go about doing something like this.

出于某种原因,我对如何去做这样的事情一无所知。

I have a table that looks like this:

我有一个看起来像这样的表:

UserID   |  Name   |  DateAdded   |   LastUpated
--------------------------------------------------
1        | James Q | 1/1/2009     |

If I insert or update record the lastupdated field should be updated the sysdate. How would I go about doing something like this?

如果我插入或更新记录 lastupdated 字段应该更新系统日期。我将如何去做这样的事情?

回答by Klaus Byskov Pedersen

CREATE OR REPLACE TRIGGER your_trigger_name
BEFORE INSERT OR UPDATE
ON your_table
FOR EACH ROW
DECLARE
BEGIN
   :new.LastUpdated := sysdate;
END;

Try that. I did not have a oracle server at hand, but I hope I got the syntax right.

试试那个。我手头没有 oracle 服务器,但我希望我的语法正确。

回答by AlteredConcept

create or replace trigger mytable_bi before insert on mytable for each row begin

在为每行开始插入 mytable 之前创建或替换触发器 mytable_bi

:NEW.lastupdated := sysdate();

:NEW.lastupdated := sysdate();

end;

结尾;

create or replace trigger mytable_bu before update on mytable for each row begin

在每行开始更新 mytable 之前创建或替换触发器 mytable_bu

:NEW.lastupdated := sysdate();

:NEW.lastupdated := sysdate();

end;

结尾;