Oracle 9i 自动增量触发器/序列不起作用

发布于 2024-12-02 22:06:59 字数 503 浏览 4 评论 0原文

在 Oracle 9i 服务器上执行以下 SQL 时,会产生错误“ORA-04098:触发器 'DBO.WTF_TRIGGER' 无效且重新验证失败”。

DROP TABLE "DBO".WTF;

CREATE TABLE "DBO".WTF
(id NUMBER PRIMARY KEY,
name VARCHAR2(30));

CREATE SEQUENCE "DBO".WTF_sequence
START WITH 1
INCREMENT BY 1;

CREATE OR REPLACE TRIGGER "DBO".WTF_trigger
BEFORE INSERT
ON "DBO".WTF
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT "DBO".WTF_sequence.nextval INTO :NEW.id FROM dual;
END;

INSERT INTO "DBO".WTF (name) VALUES ('asd');

有什么想法吗?

The following SQL, upon being executed on an Oracle 9i server, yields the the error " ORA-04098: trigger 'DBO.WTF_TRIGGER' is invalid and failed re-validation".

DROP TABLE "DBO".WTF;

CREATE TABLE "DBO".WTF
(id NUMBER PRIMARY KEY,
name VARCHAR2(30));

CREATE SEQUENCE "DBO".WTF_sequence
START WITH 1
INCREMENT BY 1;

CREATE OR REPLACE TRIGGER "DBO".WTF_trigger
BEFORE INSERT
ON "DBO".WTF
REFERENCING NEW AS NEW
FOR EACH ROW
BEGIN
SELECT "DBO".WTF_sequence.nextval INTO :NEW.id FROM dual;
END;

INSERT INTO "DBO".WTF (name) VALUES ('asd');

Any ideas?

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

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

发布评论

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

评论(1

不爱素颜 2024-12-09 22:06:59

正如 APC 指出的那样,在 SQL*Plus 中执行 SHOW ERRORS 来打印错误会很有帮助。如果我创建一个具有适当权限的 DBO 用户,您发布的代码非常适合我。

SQL> conn / as sysdba
Connected.
SQL> create user dbo identified by dbo;

User created.

SQL> grant connect, resource, unlimited tablespace to dbo;

Grant succeeded.

SQL> conn dbo/dbo
Connected.
SQL> DROP TABLE "DBO".WTF;
DROP TABLE "DBO".WTF
                 *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL>
SQL> CREATE TABLE "DBO".WTF
  2  (id NUMBER PRIMARY KEY,
  3  name VARCHAR2(30));

Table created.

SQL>
SQL> CREATE SEQUENCE "DBO".WTF_sequence
  2  START WITH 1
  3  INCREMENT BY 1;

Sequence created.

SQL>
SQL> CREATE OR REPLACE TRIGGER "DBO".WTF_trigger
  2  BEFORE INSERT
  3  ON "DBO".WTF
  4  REFERENCING NEW AS NEW
  5  FOR EACH ROW
  6  BEGIN
  7  SELECT "DBO".WTF_sequence.nextval INTO :NEW.id FROM dual;
  8  END;
  9  /

Trigger created.

SQL> INSERT INTO "DBO".WTF (name) VALUES ('asd');

1 row created.

SQL> select * from wtf;

        ID NAME
---------- ------------------------------
         1 asd

As APC points out, it would be helpful to do a SHOW ERRORS in SQL*Plus to print out the errors. The code you posted works perfectly for me if I create a DBO user with appropriate privileges.

SQL> conn / as sysdba
Connected.
SQL> create user dbo identified by dbo;

User created.

SQL> grant connect, resource, unlimited tablespace to dbo;

Grant succeeded.

SQL> conn dbo/dbo
Connected.
SQL> DROP TABLE "DBO".WTF;
DROP TABLE "DBO".WTF
                 *
ERROR at line 1:
ORA-00942: table or view does not exist


SQL>
SQL> CREATE TABLE "DBO".WTF
  2  (id NUMBER PRIMARY KEY,
  3  name VARCHAR2(30));

Table created.

SQL>
SQL> CREATE SEQUENCE "DBO".WTF_sequence
  2  START WITH 1
  3  INCREMENT BY 1;

Sequence created.

SQL>
SQL> CREATE OR REPLACE TRIGGER "DBO".WTF_trigger
  2  BEFORE INSERT
  3  ON "DBO".WTF
  4  REFERENCING NEW AS NEW
  5  FOR EACH ROW
  6  BEGIN
  7  SELECT "DBO".WTF_sequence.nextval INTO :NEW.id FROM dual;
  8  END;
  9  /

Trigger created.

SQL> INSERT INTO "DBO".WTF (name) VALUES ('asd');

1 row created.

SQL> select * from wtf;

        ID NAME
---------- ------------------------------
         1 asd
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文