什么是错的......?请检查我的触发器
create or replace
TRIGGER TBL_AE_BI
BEFORE UPDATE
ON TBL_AE
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
KD_AE VARCHAR2 (6);
BEGIN
SELECT AE_ID INTO KD_AE FROM (
SELECT KDMAX,(SELECT TO_CHAR('AE')KD FROM DUAL)||
(CASE WHEN KDMAX>=0 AND KDMAX<10 THEN '000'||KDMAX
WHEN KDMAX>=10 AND KDMAX<100 THEN '00'||KDMAX
WHEN KDMAX>=100 AND KDMAX<1000 THEN '0'||KDMAX
ELSE TO_CHAR(KDMAX)
END)AE_ID
FROM (SELECT (NVL(MAX(TO_NUMBER(SUBSTR(AE_ID,3,4))),0)+1)KDMAX FROM TBL_AE)
);
:NEW.AE_ID:=KD_AE;
:NEW.INSERT_DATE:= SYSDATE;
END;
当我在表中插入新记录时,结果是错误,无法向AE_ID插入NULL。
但是当我使用以下代码在SQL窗口中执行时
SELECT AE_ID as KD_AE FROM (
SELECT KDMAX,(SELECT TO_CHAR('AE')KD FROM DUAL)||
(CASE WHEN KDMAX>=0 AND KDMAX<10 THEN '000'||KDMAX
WHEN KDMAX>=10 AND KDMAX<100 THEN '00'||KDMAX
WHEN KDMAX>=100 AND KDMAX<1000 THEN '0'||KDMAX
ELSE TO_CHAR(KDMAX)
END)AE_ID
FROM (SELECT (NVL(MAX(TO_NUMBER(SUBSTR(AE_ID,3,4))),0)+1)KDMAX FROM TBL_AE)
)
结果没有错误,我得到一个新的唯一ID。
喜欢
答案 0 :(得分:1)
在更新之前更改为INSFORE
create or replace
TRIGGER TBL_AE_BI
BEFORE INSERT --Changed
ON TBL_AE
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
KD_AE VARCHAR2 (6);
BEGIN
SELECT AE_ID INTO KD_AE FROM (
SELECT KDMAX,(SELECT TO_CHAR('AE')KD FROM DUAL)||
(CASE WHEN KDMAX>=0 AND KDMAX<10 THEN '000'||KDMAX
WHEN KDMAX>=10 AND KDMAX<100 THEN '00'||KDMAX
WHEN KDMAX>=100 AND KDMAX<1000 THEN '0'||KDMAX
ELSE TO_CHAR(KDMAX)
END)AE_ID
FROM (SELECT (NVL(MAX(TO_NUMBER(SUBSTR(AE_ID,3,4))),0)+1)KDMAX FROM TBL_AE)
);
:NEW.AE_ID:=KD_AE;
:NEW.INSERT_DATE:= SYSDATE;
END;