【发布时间】:2016-01-27 21:44:35
【问题描述】:
我正在尝试做一个触发器,在插入语句之后将一些插入到列中。例如,我有一个表格,其中的列看起来:
Column1 Column2 Column3
我将数据插入到第 1 列,插入到表(列 1)值('256234','234234')。 现在我想自动插入 COlumn2 TImestamp 和 Column3 值“Y”,所以输出应该看起来:
Column1 Column2 Column3
256234 2015-10-28 08:48 Y
234234 2015-10-28 08:48 Y
伙计们,你们能帮帮我吗?我尝试使用光标
最后我得到了类似的东西:
create or replace trigger name
after insert on table
declare
c1 sys_refcursor;
idx varchar2(200);
begin
open c1 for select Column1 from table ;
loop
fetch c1 into idx;
exit when c1%NOTFOUND;
update table a1 set a1.Column2 = (select to_char(sysdate,'YYYYMMDDHHMISS') from dual) where Column1=idx;
update table a1 set a1.Column3 = (select 'Y' from dual) where Column1=idx;
end loop;
close c1;
end;
它工作正常,但我想知道是否有其他更好的解决方案?
【问题讨论】:
-
如果您要插入到您要插入的同一个表中,那么您可以使用
:new.columnName语法