我是ORACLE SQL的新手,我正在尝试创建一个触发器来生成auto_incement序列号作为我表中的主键,但不知道我正在制作什么错误,因为我得到了:
执行部分保存事件时出现错误:错误代码= 4098 ST = java.sql.BatchUpdateException:ORA-04098:trigger 'TNTCA.STAGING_TRIGGER_ERROR_REASON'无效且失败 重新验证\ n,由以下原因引起:\“ORA-04098:触发器 'TNTCA.STAGING_TRIGGER_ERROR_REASON'无效且失败 再验证\ n \“
我的SQL语句是:
create global temporary table STAGING_ERROR_REASON ( STAGING_REASON_ID number(4) constraint STAGING_REASON_ID not null ,REASON nvarchar2(1024) not null ,constraint PK_STAGING_REASON_ID PRIMARY KEY(STAGING_REASON_ID) );
create sequence STAGING_SEQ_ERROR_REASON start with 1 increment by 1 minvalue 1 maxvalue 10000;
CREATE OR REPLACE TRIGGER "STAGING_TRIGGER_ERROR_REASON"
BEFORE INSERT ON STAGING_ERROR_REASON
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
if(:NEW.STAGING_REASON_ID is null) then
SELECT STAGING_SEQ_ERROR_REASON.NEXTVAL
INTO :NEW.STAGING_REASON_ID
END;
ALTER TRIGGER "STAGING_TRIGGER_ERROR_REASON" ENABLE;
答案 0 :(得分:1)
您有两个错误:
IF
没有END IF
SELECT INTO
没有FROM
但是开始时不需要选择,您可以简单地分配序列值:
CREATE OR REPLACE TRIGGER "STAGING_TRIGGER_ERROR_REASON"
BEFORE INSERT ON STAGING_ERROR_REASON
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
if :NEW.STAGING_REASON_ID is null then
:NEW.STAGING_REASON_ID := STAGING_SEQ_ERROR_REASON.NEXTVAL;
END IF; --<< missing
END;
/
不确定您运行PL / SQL的方式和位置,但您通常还会在PL / SQL块的末尾need /
。
答案 1 :(得分:0)
在Oracle中,每个SELECT语句都应该有一个FROM子句。你应该至少使用
SELECT STAGING_SEQ_ERROR_REASON.NEXTVAL INTO :NEW.STAGING_REASON_ID FROM DUAL;
答案 2 :(得分:0)
您可以使用:
CREATE OR REPLACE TRIGGER "STAGING_TRIGGER_ERROR_REASON"
BEFORE INSERT ON STAGING_ERROR_REASON REFERENCING NEW
AS NEW FOR EACH ROW
BEGIN
if(:NEW.STAGING_REASON_ID is null) then
:NEW.STAGING_REASON_ID := (SELECT STAGING_SEQ_ERROR_REASON.NEXTVAL FROM dual);
end if;
END;