直接UPDATE不行吗,为什么要用动态SQL?
解决方案 »
- 如何比较"啊,天,安"中的最小值呢??
- 变量绑定VARIABLE v_output_my integer出错,请求大家帮助。
- HQL count(*)和 fetch 的问题
- 问一个很低级的问题,关于创建database links 时密码为数字(比如:1)的时候创建不上
- oracle 如何記錄delete訊息?
- 各位高手,下面的存储过程错在哪了?解决马上散分,先谢谢了
- 深入浅出SQL系列教程 第四节 SELECT命令中的GROUP BY 和 HAVING 子句
- 一个关于Decode( )转换数据视图的问题
- schema & user
- ORACLE的触发器如何调用存储过程!急!
- plsql联oracle92报错:Error while trying to retrive text for error ORA-01019
- sql语句求解?
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分钟
谢谢,需要更新的是:比如新插入的一条数据,只有stationid,obstime,inserttime和rainfall,需要在插入的同时累加前十分钟的rainfall的量插入rainfallm10。是我没表达清楚么?你的我试了下,插入新数据还是没有计算累加并插入rainfallm10,请教?
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>
谢谢,需要更新的是:比如新插入的一条数据,只有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;
那是因为before insert时该条数据本身并没有进数据库,自然查不出来,如果想包含该数据,对trigger做细小修改就可以了。
select sum(rainfall) into :new.rainfall10
->
select sum(rainfall) + :new.rainfall into :new.rainfall10
那是因为before insert时该条数据本身并没有进数据库,自然查不出来,如果想包含该数据,对trigger做细小修改就可以了。
select sum(rainfall) into :new.rainfall10
->
select sum(rainfall) + :new.rainfall into :new.rainfall10
谢谢
那是因为before insert时该条数据本身并没有进数据库,自然查不出来,如果想包含该数据,对trigger做细小修改就可以了。
select sum(rainfall) into :new.rainfall10
->
select sum(rainfall) + :new.rainfall into :new.rainfall10
现在可以了,可是有出现了新问题,如果我新插入一行数据中的stationid是之前没有的比如2,那这一行就不会计算累加,rainfallm10是空的,再插入一行stationid是2的数据,rainfallm10才有数据,怎么办?
因为你的条件里面用了t2.stationid = new.stationid,当然只有相同stationid才会匹配。
建议先了解需求
因为你的条件里面用了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;
因为你的条件里面用了t2.stationid = new.stationid,当然只有相同stationid才会匹配。
建议先了解需求
是不是应该用not exists?或者其他的? 自己还是没搞定
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;有错误,执行不了,真心求指导,谢谢!
需求是?
1。不管stationid是多少只是累加过去十分钟的值
2。根据stationid,累加相同sationid过去十分钟的值
需求是?
1。不管stationid是多少只是累加过去十分钟的值
2。根据stationid,累加相同sationid过去十分钟的值是2,只累加相同stationid的,现在的问题是,如果新插入数据的stationid是之前没有的,rainfallm10就是空值,而需要的是将rainfall的值插入rainfallm10中,不知道表达清楚了没,谢谢
恩,了解了,试试这个:
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;
/
恩,了解了,试试这个:
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;
/
可以了,非常感谢!