执行期间触发遇到的错误

时间:2018-10-11 06:49:57

标签: oracle spring-mvc plsql database-trigger

我的一项更新功能无法执行,因为以下触发器引发了异常ORA-2001,ORA-06512和ORA-04088

create or replace TRIGGER "UAM_USER"."BEFORE_UPDATE_VILLAGE" BEFORE UPDATE ON village
FOR EACH ROW
DECLARE  
    v_tbl_id  NUMBER(2);  
    user_xcep EXCEPTION;
    PRAGMA EXCEPTION_INIT( user_xcep, -20001 );
BEGIN

   IF :OLD.panchayat_id != :NEW.panchayat_id
   THEN
   RAISE user_xcep;
   END IF;

        v_tbl_id := 14;

    IF :OLD.name != :NEW.name
    THEN 

    INSERT INTO UAM_USER.MASTER_HISTORY
            (HISTORY_ID
            ,TBL_ID
            ,USER_ID
            ,FIELD_NAME
            ,OLD_VALUE
            ,NEW_VALUE
            ,HISTORY_DATE
            ,USER_NAME
            ,record_id)
    VALUES
            (MASTER_HISTORY_SEQ.NEXTVAL
            ,v_tbl_id
            ,:NEW.UPDATE_BY
            ,'Name'
            ,:OLD.name
            ,:NEW.name
            ,:NEW.UPDATE_DATE
            ,:NEW.update_by_name
            ,:NEW.village_id
            );
    END IF;

    IF (:OLD.name_hindi IS NULL AND :NEW.name_hindi IS NOT NULL)
    OR (:NEW.name_hindi IS NULL AND :OLD.name_hindi IS NOT NULL)
    OR (:OLD.name_hindi != :NEW.name_hindi)
    THEN 

    INSERT INTO UAM_USER.MASTER_HISTORY
            (HISTORY_ID
            ,TBL_ID
            ,USER_ID
            ,FIELD_NAME
            ,OLD_VALUE
            ,NEW_VALUE
            ,HISTORY_DATE
            ,USER_NAME
            ,record_id)
    VALUES
            (MASTER_HISTORY_SEQ.NEXTVAL
            ,v_tbl_id
            ,:NEW.UPDATE_BY
            ,'Name Hindi'
            ,:OLD.name_hindi
            ,:NEW.name_hindi
            ,:NEW.UPDATE_DATE
            ,:NEW.update_by_name
            ,:NEW.village_id
            );
    END IF;


IF (:OLD.status IS NULL AND :NEW.status IS NOT NULL)
    OR (:NEW.status IS NULL AND :OLD.status IS NOT NULL)
    OR (:OLD.status != :NEW.status)
    THEN 

    INSERT INTO UAM_USER.MASTER_HISTORY
            (HISTORY_ID
            ,TBL_ID
            ,USER_ID
            ,FIELD_NAME
            ,OLD_VALUE
            ,NEW_VALUE
            ,HISTORY_DATE
            ,USER_NAME
            ,record_id)
    VALUES
            (MASTER_HISTORY_SEQ.NEXTVAL
            ,v_tbl_id
            ,:NEW.UPDATE_BY
            ,'Status'
            ,:OLD.status
            ,:NEW.status
            ,:NEW.UPDATE_DATE
            ,:NEW.update_by_name
            ,:NEW.village_id
            );
    END IF;

END;

控制台日志:

org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized SQLException for SQL [ UPDATE village   SET  name = ?       ,panchayat_id = ?       ,name_hindi = ?       ,status = ?       ,update_date = ?       ,update_by = ?       ,update_by_name = ?       ,dc_id = ?       ,loksabha_id = ?       ,census_code = ?       ,pincode = ?       ,area_type = ?   WHERE village_id = ? ]; SQL state [72000]; error code [20001]; ORA-20001: 
ORA-06512: at "UAM_USER.BEFORE_UPDATE_VILLAGE", line 9
ORA-04088: error during execution of trigger 'UAM_USER.BEFORE_UPDATE_VILLAGE'
; nested exception is java.sql.SQLException: ORA-20001: 
ORA-06512: at "UAM_USER.BEFORE_UPDATE_VILLAGE", line 9
ORA-04088: error during execution of trigger 'UAM_USER.BEFORE_UPDATE_VILLAGE'

我无法弄清楚,我检查了控制器中的所有参数,所有参数都很好,控制器和服务中没有问题,问题在触发器之上,并且根据我的研发,错误代码{ {1}}和ORA-20001代表有关列宽的错误,ORA-06512代表递归更新。

更新代码:

ORA-4088

我验证我的列,一切都很好,所以剩下的就是ORA-04088,而且我无法弄清楚它在哪里以及如何递归地尝试更新表。

请帮助

2 个答案:

答案 0 :(得分:2)

当调用的过程(或触发器)失败时,

ORA-06512是标准的PL / SQL错误。

Ora-04088是标准错误,当执行中的触发器获取运行时错误时,Oracle会抛出该错误。

根本原因是ORA-20001错误。这是在用户定义的感知范围内,并且!您的触发器定义了一个:

PRAGMA EXCEPTION_INIT( user_xcep, -20001 );

触发器在这里引发此异常:

   IF :OLD.panchayat_id != :NEW.panchayat_id
   THEN
   RAISE user_xcep;
   END IF;

因此,看来您的更新语句正在尝试更改panchayat_id的值。最好的解决方案是从SET子句中删除该列:

SET  name = ?       ,panchayat_id = ? 

查看您的SpringBatch(或其他代码),也许您应该做的就是更改验证检查。也许是这样的:

   IF :OLD.panchayat_id  is null then
       null; -- allow update
   ELSIF  :NEW.panchayat_id  is null then
      :NEW.panchayat_id := :OLD.panchayat_id; -- don't wipe existing value
   ELSIF :OLD.panchayat_id != :NEW.panchayat_id
   THEN
       RAISE user_xcep;
   END IF;

答案 1 :(得分:0)

尝试通过包括           以旧换新

create or replace TRIGGER "UAM_USER"."BEFORE_UPDATE_VILLAGE" BEFORE UPDATE ON village
 REFERENCING OLD AS OLD NEW AS NEW

FOR EACH ROW