PL/SQL使用:new& :旧系统,来自DDL中不同表的条件

发布于 2025-02-12 15:37:06 字数 1187 浏览 0 评论 0原文

你好,我对触发器有相关的怀疑, DDL方面,当您对事件进行更新,并且此事件取决于两个表,则可以制作有条件的。

编辑:

问题是: - 可以将不同的表用于表更新的条件吗? - 可以在表中使用另一个表的新值? - 我可以在另一个表中使用:旧值吗? - 可以将另一个表的新列的值用作更新条件吗? - 我可以使用条件:tablename.columname =:new.tablename.columname吗?

问题在于:

Create or replace trigger TGR_example_consumer
AFTER INSERT OR UPDATE OR DELETE ON CONSUMER
FOR EACH ROW
BEGIN
     IF INSERTING THEN
        UPDATE consumption
            SET amount = amount + :NEW.amount
        WHERE ID_consumption = :NEW.ID_consumption;
        UPDATE TOTAL_consumptions
            SET amount_consumption = amount_consumption + 
               :OLD.consumption.amount;
        WHERE consumption.ID_consumption = :NEW.consumption.ID_consumption; -- > In Here is my dude.       
    ELSIF UPDATING THEN
        UPDATE consumption
            SET amount= (amount+ :OLD.amount) - :NEW.amount
        WHERE ID_consumption = :NEW.ID_consumption;
    ELSE
        UPDATE consumption
            SET amount= amount+ OLD.amount
        WHERE ID_consumption = :NEW.ID_consumption;
    END IF;
END;

在我看来,我的守时很守时,这就是我打开线程的原因。因为我没有看到类似的东西。

编辑: 我希望我已经澄清了这个问题,我想做的是在触发器中另一个表的更新下,自动更新表,几乎像库存或库存控制。

Hello I have a correlative doubt to the triggers,
and the DDL aspect, when you made an update of an event and this event depends on two tables, you can make a conditional.

EDIT:

The questions are:
-Can a different table be used for the condition of a table update?
-Can a :NEW value from another table be used in a table?
-Can I use an :OLD value from another table?
-Can the value of a :NEW column from another table be used as an update condition?
-Can I use the condition: tablename.columname = :NEW.tablename.columname ?

The issue is the following:

Create or replace trigger TGR_example_consumer
AFTER INSERT OR UPDATE OR DELETE ON CONSUMER
FOR EACH ROW
BEGIN
     IF INSERTING THEN
        UPDATE consumption
            SET amount = amount + :NEW.amount
        WHERE ID_consumption = :NEW.ID_consumption;
        UPDATE TOTAL_consumptions
            SET amount_consumption = amount_consumption + 
               :OLD.consumption.amount;
        WHERE consumption.ID_consumption = :NEW.consumption.ID_consumption; -- > In Here is my dude.       
    ELSIF UPDATING THEN
        UPDATE consumption
            SET amount= (amount+ :OLD.amount) - :NEW.amount
        WHERE ID_consumption = :NEW.ID_consumption;
    ELSE
        UPDATE consumption
            SET amount= amount+ OLD.amount
        WHERE ID_consumption = :NEW.ID_consumption;
    END IF;
END;

It seems to me that mine is very punctual that's why I opened the thread. Because I haven't seen anything similar.

EDIT:
I hope I have clarified the issue, what I want to do is under the update of another table in a trigger, update the table automatically, almost like an inventory or stock control.

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

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

发布评论

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

评论(1

飘落散花 2025-02-19 15:37:07

您显示的触发器看起来应该可以运行正常,除了在删除情况下所有:新字段为空的事实。您必须使用:旧字段。另外,您在第四个last行中有old.amount - 应该是:old.amount。另外,在第11行的末尾摆脱半彩色。

Create or replace trigger TGR_example_consumer
AFTER INSERT OR UPDATE OR DELETE ON CONSUMER
FOR EACH ROW
BEGIN
     IF INSERTING THEN
        UPDATE consumption
            SET amount = amount + :NEW.amount
        WHERE ID_consumption = :NEW.ID_consumption;
        UPDATE TOTAL_consumptions
            SET amount_consumption = amount_consumption + 
               :OLD.consumption.amount
        WHERE consumption.ID_consumption = :NEW.consumption.ID_consumption;
    ELSIF UPDATING THEN
        UPDATE consumption
            SET amount= (amount+ :OLD.amount) - :NEW.amount
        WHERE ID_consumption = :NEW.ID_consumption;
    ELSE
        UPDATE consumption
            SET amount= amount+ :OLD.amount
        WHERE ID_consumption = :OLD.ID_consumption;
    END IF;
END;

The trigger you show looks like it should run fine, except for the fact that in the DELETE case all of the :NEW fields are NULL. You'll have to use the :OLD field. Also, you have OLD.amount in the fourth-from-last line - that should be :OLD.amount. Also, get rid of the semi-colon at the end of line 11.

Create or replace trigger TGR_example_consumer
AFTER INSERT OR UPDATE OR DELETE ON CONSUMER
FOR EACH ROW
BEGIN
     IF INSERTING THEN
        UPDATE consumption
            SET amount = amount + :NEW.amount
        WHERE ID_consumption = :NEW.ID_consumption;
        UPDATE TOTAL_consumptions
            SET amount_consumption = amount_consumption + 
               :OLD.consumption.amount
        WHERE consumption.ID_consumption = :NEW.consumption.ID_consumption;
    ELSIF UPDATING THEN
        UPDATE consumption
            SET amount= (amount+ :OLD.amount) - :NEW.amount
        WHERE ID_consumption = :NEW.ID_consumption;
    ELSE
        UPDATE consumption
            SET amount= amount+ :OLD.amount
        WHERE ID_consumption = :OLD.ID_consumption;
    END IF;
END;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文