我刚刚在plsql中创建了此触发器

时间:2019-01-11 23:02:53

标签: oracle plsql

代码如下:

application/x-www-form-urlencoded

当我在桩号表中插入值时,出现以下错误:

  

插入“ DANIEL”。“ STATION”(NUM_STATION,NOM_STATION,海拔,区域)值(“ 6”,“ Baalbak”,“ 1250”,“ Baalbak”)

     

ORA-04098:触发器'DANIEL.TUPDATE_HIST_STATION'无效且重新验证失败

     

ORA-06512:在第1行

     

一次错误保存对表“ DANIEL”。“ STATION”的更改:

     

第6行:ORA-04098:触发器“ DANIEL.TUPDATE_HIST_STATION”无效且重新验证失败

     

ORA-06512:在第1行

而不是在Hist_station中插入新行

1 个答案:

答案 0 :(得分:1)

正如其他人指出的那样,存在各种错误。不过,最大的问题是,将数据插入HIST_STATION的代码不是触发器的一部分-它只是在太空中的某个地方四处游荡。

当我开始调整代码的缩进量以了解正在发生的事情时,这就是我得到的:

create or replace TRIGGER tupdate_hist_station
  AFTER INSERT ON STATION
  FOR EACH ROW
DECLARE
  V_num_station STATION.NUM_STATION%type;
  V_nb_reserve  CLIENTS.NB_RESERV%type;
  V_revenue     number;
BEGIN
    SELECT s.NUM_STATION
      INTO v_num_station
      FROM STATION s
      WHERE s.NUM_STATION =: NEW.NUM_STATION;
EXCEPTION
  WHEN no_data_found THEN
    DBMS_OUTPUT.PUT_LINE("There is no new data added");
END;

您的触发器在第一个END之后就在那里结束。其他所有代码都...消失了。

我认为您想要的是:

create or replace TRIGGER tupdate_hist_station
  AFTER INSERT ON STATION
  FOR EACH ROW
DECLARE
  V_num_station STATION.NUM_STATION%type;
  V_nb_reserve  CLIENTS.NB_RESERV%type;
  V_revenue     number;
BEGIN
  BEGIN
    SELECT s.NUM_STATION
      INTO v_num_station
      FROM STATION s
      WHERE s.NUM_STATION = :NEW.NUM_STATION;
  EXCEPTION
    WHEN no_data_found THEN
      DBMS_OUTPUT.PUT_LINE("There is no new data added");
  END;

  BEGIN
    SELECT sum(c.NB_RESERV)
      INTO V_nb_reserve 
      FROM CLIENTS c,
           RESERVATION r,
           CHAMBRE ch,
           HOTEL h,
           STATION s
      WHERE c.NUMC = r.NUMC AND
            r.IDCHAMBRE = ch.IDCHAMBRE AND
            ch.NUM_HOTEL = h.NUM_HOTEL AND
            h.NUM_STATION = s.NUM_STATION AND
            s.NUM_STATION = v_num_station;
  EXCEPTION
    WHEN no_data_found THEN
      V_nb_reserve:=0;    
      DBMS_OUTPUT.put_line("There is no reservation in a hotel in this station yet");
  END;

  BEGIN
    SELECT SUM(p.MONTANT) INTO V_revenue 
      FROM PAYEMENT p,
           CLIENTS c,
           RESERVATION r,
           CHAMBRE ch,
           HOTEL h,
           STATION s
      where p.NUMC = c.NUMC AND
            c.NUMC = r.NUMC AND
            r.IDCHAMBRE = ch.IDCHAMBRE AND
            ch.NUM_HOTEL = h.NUM_HOTEL and
            h.NUM_STATION = s.NUM_STATION AND
            s.NUM_STATION = v_num_station;    
  EXCEPTION
    WHEN no_data_found THEN
      V_revenue := 0;
      DBMS_OUTPUT.put_line("There is no reservation in this station yet");    
  END;

  INSERT INTO HIST_STATION
    ("NUM_STATION","ANNEE","NB_RESERV","REVENU")
  VALUES
    (V_num_station,(SELECT to_char(SYSDATE,'YYYY')FROM DUAL),V_nb_reserve,V_revenue);
END tupdate_hist_station;
/
SHOW ERRORS;
/

好运。