sql - 当触发器未发现任何错误时继续存储过程

标签 sql oracle stored-procedures plsql triggers

我对 Oracle 还很陌生,我想知道如果我的触发器没有检测到任何错误,如何继续运行我的过程。

如果触发器中的这两个异常没有被触发,我只想继续运行我的程序,但当我运行时它会显示此错误

ERROR at line 1:
ORA-01403: no data found
ORA-01403: no data found
ORA-06512: at "TEST.TRG_BOOKING_VALIDATION", line 9
ORA-04088: error during execution of trigger 'TEST.TRG_BOOKING_VALIDATION'
ORA-06512: at "TEST.PRC_ADD_BOOKING", line 44
ORA-06512: at line 1

程序

create sequence bookingidvalue minvalue 1 start with 3031;


CREATE OR REPLACE PROCEDURE PRC_ADD_BOOKING(CUST_ID IN NUMBER,PAYMENT_ID IN NUMBER,FRM_DEST IN VARCHAR2,TO_DEST IN VARCHAR2)AS


v_journeystatus VARCHAR2(15) := 'Pending';
v_bookeddate Booking.bookingdate%TYPE;

no_null_on_custID EXCEPTION;
no_null_on_payID EXCEPTION;
no_null_on_FROM_DEST EXCEPTION;
no_null_on_TO_DEST EXCEPTION;
duplicate_error EXCEPTION;


BEGIN

IF CUST_ID < 0
THEN
RAISE no_null_on_custID;
END IF;

IF PAYMENT_ID < 0
THEN
RAISE no_null_on_payID;
END IF;


IF FRM_DEST IS NULL
THEN
RAISE no_null_on_FROM_DEST;
END IF;

IF TO_DEST IS NULL
THEN
RAISE no_null_on_TO_DEST;
END IF;
  
IF FRM_DEST = TO_DEST
THEN
RAISE duplicate_error;
END IF;



INSERT INTO Booking values(bookingidvalue.nextval,sysdate,v_journeystatus,FRM_DEST,TO_DEST,CUST_ID,PAYMENT_ID);
DBMS_OUTPUT.PUT_LINE('Booking has been added');

EXCEPTION
WHEN no_null_on_custID then
DBMS_OUTPUT.PUT_LINE('Invalid Customer ID');

WHEN no_null_on_payID then
DBMS_OUTPUT.PUT_LINE('Invalid Payment ID');

WHEN no_null_on_FROM_DEST then
DBMS_OUTPUT.PUT_LINE('From Destination must be filled');

WHEN no_null_on_TO_DEST then
DBMS_OUTPUT.PUT_LINE('To Destination must be filled');

WHEN duplicate_error then
DBMS_OUTPUT.PUT_LINE('From destination cannot same with To destination');


END;
/

触发

CREATE OR REPLACE trigger trg_booking_validation
 before insert on booking
 for each row

 declare

 v_journeystatus booking.journeystatus%type;
 v_customerid booking.customerid%type;
 v_paymentid booking.paymentid%type;

BEGIN 

select bo.journeystatus into v_journeystatus
 from booking bo
 where bo.customerid = :new.customerid;

if SQL%FOUND then
 case
  when inserting then
   if(v_journeystatus = 'In Journey')then
    RAISE_APPLICATION_ERROR(-20950,'YOU ARE CURRENTLY IN JOURNEY, PLEASE BOOK AGAIN WHEN YOU REACHED');
   end if;
   if(v_journeystatus = 'Pending')then
    RAISE_APPLICATION_ERROR(-20950,'YOUR PREVIOUS BOOKING IS ALREADY ON PENDING! DRIVER WILL SOON PICK YOU UP, BE PATIENCE!');
   end if;
   if(v_journeystatus = NULL)then
    DBMS_OUTPUT.PUT_LINE('N0 errors');//i want to continue to my procedure here
   end if;
end case;
end if;


END;
/

最佳答案

在触发器中捕获NO_DATA_FOUND异常:

CREATE OR REPLACE trigger trg_booking_validation
before insert on booking
  for each row
declare
  v_journeystatus booking.journeystatus%type;
BEGIN 
  select bo.journeystatus
  into   v_journeystatus
  from   booking bo
  where  bo.customerid = :new.customerid;

  if v_journeystatus = 'In Journey' then
    RAISE_APPLICATION_ERROR(
      -20950,
      'YOU ARE CURRENTLY IN JOURNEY, PLEASE BOOK AGAIN WHEN YOU REACHED'
    );
  elsif v_journeystatus = 'Pending' then
    RAISE_APPLICATION_ERROR(
      -20950,
      'YOUR PREVIOUS BOOKING IS ALREADY ON PENDING! DRIVER WILL SOON PICK YOU UP, BE PATIENCE!'
    );
  end if;
EXCEPTION
  WHEN NO_DATA_FOUND THEN
    NULL;
END;
/

注意:您无需检查是否正在INSERTING,因为触发器只会在INSERT期间触发。


但是,我不确定您是否应该使用 SELECT 语句,并且不应该只检查 :NEW.journeystatus 绑定(bind)变量(如果您指的是)您正在INSERT的行:

CREATE OR REPLACE trigger trg_booking_validation
before insert on booking
  for each row
BEGIN 
  if :NEW.journeystatus = 'In Journey' then
    RAISE_APPLICATION_ERROR(
      -20950,
      'YOU ARE CURRENTLY IN JOURNEY, PLEASE BOOK AGAIN WHEN YOU REACHED'
    );
  elsif :NEW.journeystatus = 'Pending' then
    RAISE_APPLICATION_ERROR(
      -20950,
      'YOUR PREVIOUS BOOKING IS ALREADY ON PENDING! DRIVER WILL SOON PICK YOU UP, BE PATIENCE!'
    );
  end if;
END;
/

关于sql - 当触发器未发现任何错误时继续存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68883494/

相关文章:

sql-server - 更新后 SSMS 停止加载我的存储过程

javascript - 滚动加载更多 SQL 限制

php - 在 PHP、SQL 中从另一个文件访问变量

java - hibernate > CLOB > Oracle :(

sql - 更改聚合函数以在元素为 NULL 时输出 NULL

sql - 获取存储过程的源代码控制

sql - 确保两列仅包含来自同一子查询的有效结果

mysql - 查询中的许多项目的乘法计数

Oracle 参数化查询性能

mysql - 查询应返回表中不存在的值