oracle触发错误
我有两个表,表A和表B。我想设置一个触发器。一旦tableA发生插入操作,可能会触发tableB中的一些事件。
两个表如下,例如
- tableA列:(product_id,product_name,manufacturer)
- tableB列:(buyer,product_id)
我想要做的是:在表A中插入新行后,如果其product_name为null,然后触发tableB上的更新。如果表B 中的行与新插入的制造商具有相同的制造商,则将表B 的product_id 更新为该新插入的product_id。
CREATE TRIGGER t1
AFTER INSERT ON tableA
FOR EACH ROW WHEN (NEW.product_name is NULL)
BEGIN
UPDATE tableB
SET tableB.product_id = :NEW.product_id
WHERE tableB.product_id IN (SELECT tableA.product_id
FROM tableA
WHERE tableA.manufacture = :NEW.manufacture);
END;
它总是抱怨 SQL Developer 中的几个错误:
Error(2,2): PL/SQL: SQL Statement ignored
Error(2,120): PL/SQL: ORA-00933: SQL command not properly ended
Error(2,36): PL/SQL: ORA-00904: "NEW"."product_id": invalid identifier
Error: PLS-00801: internal error [ph2csql_strdef_to_diana:bind]
更新:
CREATE TABLE "tableA"
(
"PRODUCT_ID" NUMBER PRIMARY KEY,
"PRODUCT_NAME" VARCHAR2(50 BYTE) DEFAULT NULL,
"MANUFACTURE" VARCHAR2(50 BYTE) DEFAULT NULL
)
CREATE TABLE "tableB"
(
"BUYER_ID" NUMBER PRIMARY KEY,
"PRODUCT_ID" NUMBER DEFAULT NULL
)
I have two tables, tableA and tableB. I want to set a trigger. Once an insert happens in tableA, it may trigger some events in tableB.
The two tables are as follows, for example,
- tableA columns: (product_id, product_name, manufacture)
- tableB columns: (buyer, product_id)
What I want to do is: after inserting a new row into table A, if its product_name is null, then trigger updates on tableB. Update tableB' product_id to this new inserted product_id if the rows in tableB has the same manufacture as new inserted manufacture.
CREATE TRIGGER t1
AFTER INSERT ON tableA
FOR EACH ROW WHEN (NEW.product_name is NULL)
BEGIN
UPDATE tableB
SET tableB.product_id = :NEW.product_id
WHERE tableB.product_id IN (SELECT tableA.product_id
FROM tableA
WHERE tableA.manufacture = :NEW.manufacture);
END;
It always complains several errors in SQL developer:
Error(2,2): PL/SQL: SQL Statement ignored
Error(2,120): PL/SQL: ORA-00933: SQL command not properly ended
Error(2,36): PL/SQL: ORA-00904: "NEW"."product_id": invalid identifier
Error: PLS-00801: internal error [ph2csql_strdef_to_diana:bind]
update:
CREATE TABLE "tableA"
(
"PRODUCT_ID" NUMBER PRIMARY KEY,
"PRODUCT_NAME" VARCHAR2(50 BYTE) DEFAULT NULL,
"MANUFACTURE" VARCHAR2(50 BYTE) DEFAULT NULL
)
CREATE TABLE "tableB"
(
"BUYER_ID" NUMBER PRIMARY KEY,
"PRODUCT_ID" NUMBER DEFAULT NULL
)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您是同时遇到所有这些错误,还是在尝试不同的事情时遇到不同的错误?如果您在
NEW.product_id
之前省略:
,则会出现 ORA-00904(以及可能相关的 ORA-00933),并且 PLS-00801 可能来自于中间有空格(即
:NEW.product_id
。不知道如何同时获得两者。现在发布的内容看起来不错 - 您是否仍然收到消息
错误:检查编译器
TRIGGER T1 编译后
- 或者您是否在 SQL Developer 的编译器日志窗口中查看旧错误?如果您不确定,请右键单击编译器日志窗口并选择“清除” ' 在重新运行之前,查看当前代码真正生成了哪些错误(如果有)。Are you getting all those errors at the same time, or different errors as you try different things? The ORA-00904 (and possibly associated ORA-00933) would appear if you omitted the
:
beforeNEW.product_id
and the PLS-00801 could come from having a space in between(i.e.
: NEW.product_id
. Not sure how you could get both at the same time.As it's posted now it looks fine - do you still get the message
Errors: check compiler log
afterTRIGGER T1 compiled
- or are you looking at old errors in SQL Developer's compiler log window? If you aren't sure, right-click in the compiler log window and choose 'clear' before re-running, to see what errors (if any) are really being generated by the current code.您已创建了大小写混合名称“tableA”和“tableB”的表。这在 Oracle 中通常是一种不好的做法,并且在代码中引用表时会导致问题,因为它们必须以正确的大小写引用,并用双引号引起来:
You have created the tables with mixed case names "tableA" and "tableB". This is generally a bad practice in Oracle, and leads to problems when referencing the tables in code because they must be referred to in the correct case, enclosed in double quotes:
在 SQL*Plus 中,您需要在单行上使用
/
终止CREATE TRIGGER
语句。根据您的 SQL 工具,您可能需要使用某种不同的方式来设置备用分隔符。
In SQL*Plus you need to terminate a
CREATE TRIGGER
statement with a/
on a single line.Depending on your SQL tool you might need to use some different way of setting an alternate delimiter.
修改嵌套 select 语句中的 where 子句
WHERE "tableA".manufacture = :NEW.manufacture
到
WHERE "tableA".manufacture = NEW.manufacture
Modify the where clause in your nested select statement from
WHERE "tableA".manufacture = :NEW.manufacture
to
WHERE "tableA".manufacture = NEW.manufacture