日期:2014-05-16  浏览次数:20831 次

求助,关于触发器
现在有表结构如下:
STATIONID   OBSTIME       INSERTTIME    RAINFALL    RAINFALLM10
1 2013/7/1 0:01:00 2013/7/1 0:05:00 1.00
2 2013/7/1 0:02:00 2013/7/1 0:05:00 2.00
3 2013/7/1 0:03:00 2013/7/1 0:05:00 3.00
.
.
.

stationid 和 obstime 是联合主键pk_rf1,现需要写一个触发器在本表插入新数据的时候,根据stationid和obstime自动累加过去十分钟rainfall的值并插入rainfallm10,自己写了一个触发器,可是不行,来求助各位,谢谢了!
我自己在网上到处搜,然后拼凑的,错在什么地方?怎么解决?
create or replace trigger aifer_rf1
before  update 
of obstime 
on rf1 for each row
begin
execute immediate 
'update rf1 set :new.rainfallm10=(
select sum(rainfall) from rf1 t2 where t2.obstime between rf1.obstime-9/1440 and rf1.obstime and rf1.stationid=t2.stationid and :new.pk_rf1 = :old.pk_rf1
)';
end;

------解决方案--------------------

create or replace trigger aifer_rf1
before  update 
of obstime 
on rf1 for each row
begin
  :new.rainfallm10 := (
select sum(rainfall) from rf1 t2 
where t2.obstime between :new.obstime-1/144 and :new.obstime 
and t2.stationid=:new.stationid
)
end;
--不知道楼主需要跟新的到底是那些数据
--以上是跟新本条记录的
--ps:1/144才是10分钟

------解决方案--------------------
SQL> CREATE TABLE rf1(stationid number, obstime date, inserttime date, rainfall number, rainfall10 number);
 
Table created
SQL> insert into rf1 values(1,to_date('20130701 00:01:00','yyyymmdd HH24:MI:SS'), sysdate, 1, 0);
 
1 row inserted
SQL> insert into rf1 values(1,to_date('20130701 00:02:00','yyyymmdd HH24:MI:SS'), sysdate, 1, 0);
 
1 row inserted
SQL> create or replace trigger aifer_rf1
  2  before  insert
  3  on rf1 for each row
  4  begin
  5  select sum(rainfall) into :new.rainfall10  from rf1 t2
  6  where t2.obstime between :new.obstime-9/1440 and :new.obstime
  7  and t2.stationid=:new.stationid
  8  ;
  9  end;
 10  /
 
Trigger created
SQL> insert into