直接UPDATE不行吗,为什么要用动态SQL?

解决方案 »

  1.   


    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分钟
      

  2.   

    直接UPDATE不行吗,为什么要用动态SQL?直接UPDATE,插入新数据的时候没有累加前十分钟的量,不明白为什么,后来网上搜说用execute immediate,加了也不行,试了好久
      

  3.   


    谢谢,需要更新的是:比如新插入的一条数据,只有stationid,obstime,inserttime和rainfall,需要在插入的同时累加前十分钟的rainfall的量插入rainfallm10。是我没表达清楚么?你的我试了下,插入新数据还是没有计算累加并插入rainfallm10,请教?
      

  4.   

    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 rf1 values(1,to_date('20130701 00:03:00','yyyymmdd HH24:MI:SS'), sysdate, 1, 0);
     
    1 row inserted
    SQL> select * from rf1;
     
     STATIONID OBSTIME     INSERTTIME    RAINFALL RAINFALL10
    ---------- ----------- ----------- ---------- ----------
             1 7/1/2013 12 12/19/2013           1          0
             1 7/1/2013 12 12/19/2013           1          0
             1 7/1/2013 12 12/19/2013           1          2
     
    SQL> drop table rf1;
     
    Table dropped
     
    SQL> 
      

  5.   


    谢谢,需要更新的是:比如新插入的一条数据,只有stationid,obstime,inserttime和rainfall,需要在插入的同时累加前十分钟的rainfall的量插入rainfallm10。是我没表达清楚么?你的我试了下,插入新数据还是没有计算累加并插入rainfallm10,请教?

    插入数据的触发器,我勒个去...(前面你写的是更新的触发器)create or replace trigger aifer_rf1
    before insert 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;
      

  6.   

    这个需要用到“自动事务”,建议楼主先熟悉oracle的“自治事务”!
      

  7.   

    哦,忘了,用select into OK.
      

  8.   


    那是因为before insert时该条数据本身并没有进数据库,自然查不出来,如果想包含该数据,对trigger做细小修改就可以了。
    select sum(rainfall) into :new.rainfall10
    ->
    select sum(rainfall)  + :new.rainfall into :new.rainfall10
      

  9.   


    那是因为before insert时该条数据本身并没有进数据库,自然查不出来,如果想包含该数据,对trigger做细小修改就可以了。
    select sum(rainfall) into :new.rainfall10
    ->
    select sum(rainfall)  + :new.rainfall into :new.rainfall10
    谢谢
      

  10.   


    那是因为before insert时该条数据本身并没有进数据库,自然查不出来,如果想包含该数据,对trigger做细小修改就可以了。
    select sum(rainfall) into :new.rainfall10
    ->
    select sum(rainfall)  + :new.rainfall into :new.rainfall10
    现在可以了,可是有出现了新问题,如果我新插入一行数据中的stationid是之前没有的比如2,那这一行就不会计算累加,rainfallm10是空的,再插入一行stationid是2的数据,rainfallm10才有数据,怎么办?
      

  11.   


    因为你的条件里面用了t2.stationid = new.stationid,当然只有相同stationid才会匹配。
    建议先了解需求
      

  12.   


    因为你的条件里面用了t2.stationid = new.stationid,当然只有相同stationid才会匹配。
    建议先了解需求
    谢谢,我加了个if else,好像还是不行,求助?
    create or replace trigger aifer_rf1
    before  insert
    on rf1  for each row
    begin
    if  :new.stationid < > t2.stationid then 
    select :new.rainfall into :new.rainfallm10 from rf1 t2;
    else
    select sum(rainfall) +:new.rainfall into :new.rainfallm10  from rf1 
    where t2.obstime between :new.obstime-9/1440 and :new.obstime
    and t2.stationid=:new.stationid
    ;
    end if;
    end;
      

  13.   


    因为你的条件里面用了t2.stationid = new.stationid,当然只有相同stationid才会匹配。
    建议先了解需求
    是不是应该用not exists?或者其他的? 自己还是没搞定
      

  14.   

    create or replace trigger aifer_rf1
    before  insert
    on rf1  for each row
    begin
    if not exists ( select * from rf1 t2 where t2.stationid=:new.stationid )then 
    select :new.rainfall into :new.rainfallm10 from rf1 ;
    else
    select sum(rainfall) +:new.rainfall into :new.rainfallm10  from rf1 
    where t2.obstime between :new.obstime-9/1440 and :new.obstime
    and t2.stationid=:new.stationid
    ;
    end if;
    end;有错误,执行不了,真心求指导,谢谢!
      

  15.   


    需求是?
    1。不管stationid是多少只是累加过去十分钟的值
    2。根据stationid,累加相同sationid过去十分钟的值
      

  16.   


    需求是?
    1。不管stationid是多少只是累加过去十分钟的值
    2。根据stationid,累加相同sationid过去十分钟的值是2,只累加相同stationid的,现在的问题是,如果新插入数据的stationid是之前没有的,rainfallm10就是空值,而需要的是将rainfall的值插入rainfallm10中,不知道表达清楚了没,谢谢
      

  17.   


    恩,了解了,试试这个:
    CREATE OR REPLACE TRIGGER aifer_rf1
      BEFORE INSERT ON rf1
      FOR EACH ROW
    BEGIN
      SELECT SUM(rainfall)
        INTO :new.rainfall10
        FROM rf1 t2
       WHERE t2.obstime BETWEEN :new.obstime - 9 / 1440 AND :new.obstime
         AND t2.stationid = :new.stationid;
      :new.rainfall10 := nvl(:new.rainfall10, 0) + nvl(:new.rainfall, 0);
    END;
    /
      

  18.   


    恩,了解了,试试这个:
    CREATE OR REPLACE TRIGGER aifer_rf1
      BEFORE INSERT ON rf1
      FOR EACH ROW
    BEGIN
      SELECT SUM(rainfall)
        INTO :new.rainfall10
        FROM rf1 t2
       WHERE t2.obstime BETWEEN :new.obstime - 9 / 1440 AND :new.obstime
         AND t2.stationid = :new.stationid;
      :new.rainfall10 := nvl(:new.rainfall10, 0) + nvl(:new.rainfall, 0);
    END;
    /

    可以了,非常感谢!