gpt4 book ai didi

sql - 触发器无效,重新验证失败

转载 作者:行者123 更新时间:2023-12-04 16:04:00 25 4
gpt4 key购买 nike

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

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

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

最佳答案

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



这是您的触发器编译失败的地方。
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 .

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

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

关于sql - 触发器无效,重新验证失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3440499/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com