关于PL/SQL触发器的另一个问题

发布于 2024-10-30 09:30:17 字数 721 浏览 0 评论 0原文

我遇到了第二个触发器的问题,我们要编写以下内容:

我刚刚编写了一个存储过程和存储函数,用于将新行插入到我的 Orders 表中。行更新插入:Ordernum、OrderDate、Customer、Rep、Manufacturer、Product、Qty 和 SaleAmount。

我现在必须编写一个触发器,通过添加新添加的销售金额来更新我的 Offices 表。问题是,并非每个销售代表都分配有一个办公室。我不明白我是否需要在“FOR EACH ROW”下有一个以某种方式规定这一点的when子句,或者我是否需要在我的SET Sales行之后进行规定。到目前为止,这是我的代码,但不幸的是,它更新了所有办公室销售,而不仅仅是销售代表所属的销售:

CREATE OR REPLACE TRIGGER UpdateOffices
   AFTER INSERT ON Orders
   FOR EACH ROW
   BEGIN 
      UPDATE Offices
      SET Sales = Sales + :NEW.Amount
      WHERE Office IN (SELECT RepOffice
                       FROM Salesreps
                       WHERE RepOffice IS NOT NULL);
End;
/
SHOW ERRORS

Sales 是 Offices 表上的列的名称。存储过程中使用的名称的数量。

I have run into a problem with a second trigger we are to write regarding the following:

I have just written a stored procedure and stored function that serve to insert a new row into my Orders table. The row update inserts: Ordernum, OrderDate, Customer, Rep, Manufacturer, Product, Qty, and SaleAmount.

I now have to write a trigger that updates my Offices table by adding the amount of the newly added sale. Problem is, not every salesrep has an office assigned to them. I don't understand if I need to have a when clause under 'FOR EACH ROW' which somehow stipulates this, or if I need to stipulate after my SET Sales line. This is my code so far, but unfortunately, it updates all of the offices sales, not just the one that the salesrep belongs to:

CREATE OR REPLACE TRIGGER UpdateOffices
   AFTER INSERT ON Orders
   FOR EACH ROW
   BEGIN 
      UPDATE Offices
      SET Sales = Sales + :NEW.Amount
      WHERE Office IN (SELECT RepOffice
                       FROM Salesreps
                       WHERE RepOffice IS NOT NULL);
End;
/
SHOW ERRORS

Sales is the name of the column on the Offices table. Amount if the name used in the stored proc.

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

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

发布评论

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

评论(2

一腔孤↑勇 2024-11-06 09:30:17

我们已经建议您在 Salesrep 上添加谓词,例如 WHERE Salesrep = :NEW.Rep。为什么不将其添加到子查询中?

You've already been advised to add a predicate on Salesreps, e.g. WHERE Salesrep = :NEW.Rep. Why don't you add it to the subquery?

2024-11-06 09:30:17

使用此触发方法实施解决方案时,您将遇到各种问题。

一旦订单修改提交到数据库,Office.Sales 列值就会变得过时。例如,当订单的价格或数量发生更改或删除时。

我建议您将此要求实现为“提交时刷新”物化视图。

You are going to run into all sorts of problems implementing your solution using this trigger method.

The Office.Sales column value will become stale as soon as amendments to orders are committed to the database. For example, when the price or quantity on an order is changed or deleted.

I would recommend you implement this requirement as a 'Refresh on Commit' Materialized View.

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