您好,欢迎访问一九零五行业门户网

Oracle触发器中select into 报错no_data_found异常处理

oracle触发器中select into 报错no_data_found异常处理
红色部分为对查询不到数据异常的处理
create or replace trigger tig_monitor_alarm
  after insert on t_monitor_real_minute 
  for each row
declare
  -- 标准值
  standvalue number;
  --报警实况表id
  liveid number;
begin
  --
  --触发器功能:监测实况数据表,,对比监测数据是否超标,超标数据则记录入超标报警表中
  --
  standvalue:=-1;
  liveid:=-1;
  select nvl(t.bzz,-1) into standvalue from t_monitor_factor t where t.jcdbm=:new.station_id and t.jcxmbm=:new.infectant_id;
--如果录入检测项目数据大于标准值,则入库报警信息表中
  if standvalue>-1 then
    if :new.m_value>standvalue then
       --将数据录入报警历史数据中
       insert into t_alarm_history(id,jcdbm,jcxmbm,mvalue,mtime)
       values(seq_alarm_history.nextval,:new.station_id,:new.infectant_id,:new.m_value,:new.m_time);
--异常判断,如果查询不到数据
       begin
       select r.id into liveid from t_alarm_real r where r.jcdbm=:new.station_id and r.jcxmbm=:new.infectant_id;
       --查询不到数据
       exception
          when no_data_found then
             --不存在则录入新的报警实况
          insert into t_alarm_history(id,jcdbm,jcxmbm,mvalue,mtime)
          values(seq_alarm_real.nextval,:new.station_id,:new.infectant_id,:new.m_value,:new.m_time);
        end;
       --报警实况中是否已存在该监测点的该因子报警信息
       if liveid>-1 then
          update t_alarm_real r1 set r1.mvalue=:new.m_value,r1.mtime=:new.m_time,r1.status=0 where r1.id=liveid;
       else
          --不存在则录入新的报警实况
          insert into t_alarm_history(id,jcdbm,jcxmbm,mvalue,mtime)
          values(seq_alarm_real.nextval,:new.station_id,:new.infectant_id,:new.m_value,:new.m_time);
       end if;
    end if;
  end if;
exception
          when no_data_found then
             null;
end tig_monitor_alarm;
其它类似信息

推荐信息