【问题标题】:Oracle: Trigger to INSERT into one table changes from separate tableOracle:触发器从单独的表插入到一个表的更改
【发布时间】:2019-01-04 03:20:34
【问题描述】:

我有两张表,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.

请让我知道您的建议,我已经搜索了高低,但无法找到解决方案。

【问题讨论】:

  • 你试过SHOW ERRORS TRIGGER price_hist; 吗?另外,为什么您的错误显示触发器的名称如“KEVIN.PRICE_UPDATE”,而在代码中它是“price_hist”,是不是有两个触发器?
  • 您的架构中有两个触发器吗?

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


【解决方案1】:

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

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

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

我检查了我的架构,一切都很顺利,所以你的数据库架构可能有问题。

尝试在另一个用户上创建相同的表,看看问题是否会重复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-14
    • 2020-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-15
    相关资源
    最近更新 更多