Home > Database > Mysql Tutorial > Oracle触发器中select into 报错no_data_found异常处理

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

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-07 17:19:10
Original
1239 people have browsed it

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;

linux

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template