Oracle:触发将INSERT插入到一个表中的更改(来自单独的表)

时间:2018-07-27 03:50:00

标签: sql oracle triggers sql-update sql-insert

我有两个表,item和item_price_history。我想创建一个触发器,在价格表中更改项目价格后插入旧价格的记录。这是两个表:

CREATE TABLE item(
item_id DECIMAL(10) NOT NULL,
description VARCHAR(30),
price DECIMAL(10),
PRIMARY KEY (item_id));


CREATE TABLE item_price_history(
item_id DECIMAL(10) NOT NULL,
old_price DECIMAL(10) NOT NULL,
new_price DECIMAL(10) NOT NULL,
date_of_change DATE NOT NULL,
FOREIGN KEY (item_id) references ITEM);

我已经尝试了多种方法来使其编译,但这就是我的触发条件:

CREATE OR REPLACE TRIGGER price_hist
AFTER UPDATE ON item
FOR EACH ROW
WHEN (new.price <> old.price)

BEGIN
INSERT INTO item_price_history(item_id, old_price, new_price, date_of_change)
VALUES (:NEW.item_id, :OLD.price, :NEW.price, SYSDATE);
END;

更新商品价格后,我收到以下错误消息:

Error starting at line : 1 in command -
UPDATE item
SET price = 11
WHERE item_id = 1
Error at Command Line : 1 Column : 8
Error report -
SQL Error: ORA-04098: trigger 'KEVIN.PRICE_UPDATE' 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.

请让我知道您的建议,我搜索过很多次,但找不到解决方法。

1 个答案:

答案 0 :(得分:1)

此错误仅表示触发器存在问题。 您将需要查看 USER_ERROR

select *
from
   user_errors
where
   type = 'TRIGGER'
and
   name = 'price_hist';

也检查SHOW ERRORS TRIGGER price_hist. 您应该从此来源获得更多信息。原因可能很多,主要是由于对象上的错误所致。

我检查了自己的架构,一切都进行得很顺利,所以您的数据库架构可能出了点问题。

尝试在另一个用户上创建相同的表,看看问题是否再次发生。

相关问题