触发器无效且重新验证失败

发布于 2024-09-14 05:20:35 字数 2577 浏览 5 评论 0原文

这是我用来创建表、序列和触发器的代码

DROP TABLE CDR.ExtDL_JobStatus;

-- 
-- TABLE: CDR.ExtDL_JobStatus 
--

CREATE TABLE CDR.ExtDL_JobStatus(
    Id             NUMBER(38, 0)    NOT NULL,
    ShortName      NUMBER(38, 0)    NOT NULL,
    Description    NUMBER(38, 0)    NOT NULL,
    CONSTRAINT PK_ExtDL_JobStatus PRIMARY KEY (Id)
)
;



Declare NumOfSequences NUMBER :=0;
Begin
  Select COUNT(*)
  INTO NumOfSequences
  FROM All_Sequences
  WHERE 1=1
    And upper (Sequence_Owner) = upper ('CDR')
    And upper (Sequence_Name) = upper ('ExtDL_JobStatus_Seq');
  If NumOfSequences > 0 Then
    Execute IMMEDIATE 'DROP SEQUENCE CDR.ExtDL_JobStatus_Seq';
  End If;
End;
/
CREATE SEQUENCE CDR.ExtDL_JobStatus_Seq
    INCREMENT BY 1
    START WITH 1
    NOMAXVALUE 
    NOMINVALUE 
;
/

Declare NumOfTriggers NUMBER :=0;
Begin
  SELECT COUNT(*)
  INTO NumOfTriggers
  FROM All_Triggers
  WHERE 1=1
    And upper (Owner) = upper ('CDR')
    And upper (Trigger_Name) = upper ('ExtDL_JobStatus_SeqTrg');
  If NumOfTriggers > 0 Then
    Execute IMMEDIATE 'DROP SEQUENCE CDR.ExtDL_JobStatus_SeqTrg';
  End If;
End;
/
CREATE TRIGGER CDR.ExtDL_JobStatus_SeqTrg
BEFORE INSERT
ON CDR.ExtDL_JobStatus
    FOR EACH ROW
    WHEN (new.Id IS NULL)
    BEGIN
        SELECT ExtDL_JobStatus_SeqTrg.nextval into :new.Id from dual;
    END;


/
INSERT INTO ExtDL_JobStatus (Id, ShortName, Description) Values (0, 'Success', 'Fail')
/
SELECT * FROM ExtDL_JobStatus

当我执行代码时,我得到以下输出

DROP TABLE CDR.ExtDL_JobStatus succeeded.
CREATE TABLE succeeded.
anonymous block completed
CREATE SEQUENCE succeeded.
anonymous block completed
Warning: execution completed with warning
TRIGGER CDR.ExtDL_JobStatus_SeqTrg Compiled.

Error starting at line 62 in command:
INSERT INTO ExtDL_JobStatus (Id, ShortName, Description) Values (0, 'Success', 'Fail')
Error at Command Line:62 Column:12
Error report:
SQL Error: ORA-04098: trigger 'CDR.EXTDL_JOBSTATUS_SEQTRG' is invalid and failed re-validation
04098. 00000 -  "trigger '%s.%s' is invalid and failed re-validation"
*Cause:    A trigger was attempted to be retrieved for execution and was
           found to be invalid.  This also means that compilation/authorization
           failed for the trigger.
*Action:   Options are to resolve the compilation/authorization errors,
           disable the trigger, or drop the trigger.
ID                     SHORTNAME              DESCRIPTION            
---------------------- ---------------------- ---------------------- 

0 rows selected

是什么使我的触发器无效?

Here is the code that I use to create a table, a sequence and a trigger

DROP TABLE CDR.ExtDL_JobStatus;

-- 
-- TABLE: CDR.ExtDL_JobStatus 
--

CREATE TABLE CDR.ExtDL_JobStatus(
    Id             NUMBER(38, 0)    NOT NULL,
    ShortName      NUMBER(38, 0)    NOT NULL,
    Description    NUMBER(38, 0)    NOT NULL,
    CONSTRAINT PK_ExtDL_JobStatus PRIMARY KEY (Id)
)
;



Declare NumOfSequences NUMBER :=0;
Begin
  Select COUNT(*)
  INTO NumOfSequences
  FROM All_Sequences
  WHERE 1=1
    And upper (Sequence_Owner) = upper ('CDR')
    And upper (Sequence_Name) = upper ('ExtDL_JobStatus_Seq');
  If NumOfSequences > 0 Then
    Execute IMMEDIATE 'DROP SEQUENCE CDR.ExtDL_JobStatus_Seq';
  End If;
End;
/
CREATE SEQUENCE CDR.ExtDL_JobStatus_Seq
    INCREMENT BY 1
    START WITH 1
    NOMAXVALUE 
    NOMINVALUE 
;
/

Declare NumOfTriggers NUMBER :=0;
Begin
  SELECT COUNT(*)
  INTO NumOfTriggers
  FROM All_Triggers
  WHERE 1=1
    And upper (Owner) = upper ('CDR')
    And upper (Trigger_Name) = upper ('ExtDL_JobStatus_SeqTrg');
  If NumOfTriggers > 0 Then
    Execute IMMEDIATE 'DROP SEQUENCE CDR.ExtDL_JobStatus_SeqTrg';
  End If;
End;
/
CREATE TRIGGER CDR.ExtDL_JobStatus_SeqTrg
BEFORE INSERT
ON CDR.ExtDL_JobStatus
    FOR EACH ROW
    WHEN (new.Id IS NULL)
    BEGIN
        SELECT ExtDL_JobStatus_SeqTrg.nextval into :new.Id from dual;
    END;


/
INSERT INTO ExtDL_JobStatus (Id, ShortName, Description) Values (0, 'Success', 'Fail')
/
SELECT * FROM ExtDL_JobStatus

When I execute the code, I get the following output

DROP TABLE CDR.ExtDL_JobStatus succeeded.
CREATE TABLE succeeded.
anonymous block completed
CREATE SEQUENCE succeeded.
anonymous block completed
Warning: execution completed with warning
TRIGGER CDR.ExtDL_JobStatus_SeqTrg Compiled.

Error starting at line 62 in command:
INSERT INTO ExtDL_JobStatus (Id, ShortName, Description) Values (0, 'Success', 'Fail')
Error at Command Line:62 Column:12
Error report:
SQL Error: ORA-04098: trigger 'CDR.EXTDL_JOBSTATUS_SEQTRG' is invalid and failed re-validation
04098. 00000 -  "trigger '%s.%s' is invalid and failed re-validation"
*Cause:    A trigger was attempted to be retrieved for execution and was
           found to be invalid.  This also means that compilation/authorization
           failed for the trigger.
*Action:   Options are to resolve the compilation/authorization errors,
           disable the trigger, or drop the trigger.
ID                     SHORTNAME              DESCRIPTION            
---------------------- ---------------------- ---------------------- 

0 rows selected

What makes my trigger invalid?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

可可 2024-09-21 05:20:35

警告:执行完成
警告触发
CDR.ExtDL_JobStatus_SeqTrg 已编译。

这是您的触发器编译失败的地方。

sql> CREATE TRIGGER ExtDL_JobStatus_SeqTrg
  2  BEFORE INSERT
  3  ON ExtDL_JobStatus
  4      FOR EACH ROW
  5      WHEN (new.Id IS NULL)
  6      BEGIN
  7          SELECT ExtDL_JobStatus_SeqTrg.nextval into :new.Id from dual;
  8      END;
  9  /

Warning: Trigger created with compilation errors.

sql> show errors;
Errors for TRIGGER EXTDL_JOBSTATUS_SEQTRG:

LINE/COL ERROR
-------- -----------------------------------------------------------------
2/9      PL/SQL: SQL Statement ignored
2/16     PL/SQL: ORA-02289: sequence does not exist

问题是因为您在代码中使用 ExtDL_JobStatus_SeqTrg,并且您创建的序列是 ExtDL_JobStatus_Seq

另外,如果您尝试运行这样的脚本来创建(编译)对象,我建议您在每个触发器/过程/函数创建语句之后添加以下子句。

SHOW ERRORS;

如果您的语句成功,则不会产生任何错误。如果出现任何错误,您将获得错误的详细描述,而不必再次执行脚本。

Warning: execution completed with
warning TRIGGER
CDR.ExtDL_JobStatus_SeqTrg Compiled.

This is where your trigger compilation failed.

sql> CREATE TRIGGER ExtDL_JobStatus_SeqTrg
  2  BEFORE INSERT
  3  ON ExtDL_JobStatus
  4      FOR EACH ROW
  5      WHEN (new.Id IS NULL)
  6      BEGIN
  7          SELECT ExtDL_JobStatus_SeqTrg.nextval into :new.Id from dual;
  8      END;
  9  /

Warning: Trigger created with compilation errors.

sql> show errors;
Errors for TRIGGER EXTDL_JOBSTATUS_SEQTRG:

LINE/COL ERROR
-------- -----------------------------------------------------------------
2/9      PL/SQL: SQL Statement ignored
2/16     PL/SQL: ORA-02289: sequence does not exist

The problem is because you are using ExtDL_JobStatus_SeqTrg in your code and the sequence you created is ExtDL_JobStatus_Seq.

Also, if you are trying to run a script like this for creating (compiling) the objects, I would suggest you add the following clause after each trigger/procedure/function creatin statement.

SHOW ERRORS;

If your statement succceds, that will just produce no errors. If there are any erros, you'll have a detailed description of the errors instead of having to execute the script again.

梦年海沫深 2024-09-21 05:20:35

这是一个简单的拼写错误:您的序列名为 ExtDL_JobStatus_Seq,但在触发器中引用 ExtDL_JobStatus_SeqTrg.nextval

为了将来参考,最好在每次编译 PL/SQL(触发器、过程等)的调用之后在脚本中包含一个显示错误的调用。像这样:

CREATE TRIGGER CDR.ExtDL_JobStatus_SeqTrg 
BEFORE INSERT 
ON CDR.ExtDL_JobStatus 
    FOR EACH ROW 
    WHEN (new.Id IS NULL) 
    BEGIN 
        SELECT ExtDL_JobStatus_SeqTrg.nextval into :new.Id from dual; 
    END; 
/ 

show errors

顺便说一句,在试图删除序列的匿名块中也存在相同的拼写错误。

It's a simple typo: your sequence is called ExtDL_JobStatus_Seq but in your trigger you reference ExtDL_JobStatus_SeqTrg.nextval.

For future reference it is a good idea to include a call to show error in scripts after each call which compiles PL/SQL (triggers, procedures, etc). Like this:

CREATE TRIGGER CDR.ExtDL_JobStatus_SeqTrg 
BEFORE INSERT 
ON CDR.ExtDL_JobStatus 
    FOR EACH ROW 
    WHEN (new.Id IS NULL) 
    BEGIN 
        SELECT ExtDL_JobStatus_SeqTrg.nextval into :new.Id from dual; 
    END; 
/ 

show errors

Incidentally, there's the same typo in the anonymous block which attempst to drop the sequence.

冷血 2024-09-21 05:20:35

除了前面提到的所有内容之外,还有两个额外的拼写错误/错误:

  1. 尝试删除触发器的匿名 PL/SQL 块实际上显示为 DROP SEQUENCE
  2. insert 语句尝试将字符串插入到 ShortName 和 Description 列中,这两个列都定义为 NUMBER(38, 0)。

In addition to everything previously mentioned, there are two additional typos/errors:

  1. The anonymous PL/SQL block that tries to drop the trigger actually says DROP SEQUENCE.
  2. The insert statement attempts to insert characters strings into the ShortName and Description columns, which are both defined as NUMBER(38, 0).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文