[英]Oracle SQL Trigger: After Update, Insert Rows into Another Table
I am trying to write a trigger on one table that is triggered when the quantity in stock = 0. It will then add a row to another table with the product_id and store_id to indicate that item needs to be evaluated for reorder.我正在尝试在一个表上编写一个触发器,该触发器在库存数量 = 0 时触发。然后它将向另一个表中添加一行,其中包含 product_id 和 store_id,以指示需要评估该项目以进行重新排序。 After over 4 hours of searching and trying, I made an account here.
经过4个多小时的搜索和尝试,我在这里注册了一个帐户。 I'm a new user and this could be totally off.
我是新用户,这可能完全关闭。
This is my latest attempt:这是我最近的尝试:
CREATE OR REPLACE TRIGGER ORDERS_AFTER_UPDATE
AFTER UPDATE
OF QUANTITY_STOCK
ON INVENTORY
FOR EACH ROW
WHEN (NEW.QUANTITY_STOCK - OLD.QUANTITY_STOCK = 0)
BEGIN
SELECT PRODUCT_ID, STORE_ID
INTO PURCHASE_NEW
FROM INVENTORY
WHERE QUANTITY_STOCK = 0;
END;
The relevant tables are INVENTORY, having PRODUCT_ID, STORE_ID, and QUANTITY_STOCK and PURCHASE_NEW, having PRODUCT_ID and STORE_ID.相关表是 INVENTORY,具有 PRODUCT_ID、STORE_ID,以及 QUANTITY_STOCK 和 PURCHASE_NEW,具有 PRODUCT_ID 和 STORE_ID。 I want it to copy PRODUCT_ID and STORE_ID into the PURCHASE_NEW table from the INVENTORY table when the value of QUANTITY_STOCK = 0.
当 QUANTITY_STOCK 的值 = 0 时,我希望它从 INVENTORY 表中将 PRODUCT_ID 和 STORE_ID 复制到 PURCHASE_NEW 表中。
Errors I'm receiving are:我收到的错误是:
Error(8,1): PL/SQL: SQL Statement ignored
Error(9,6): PLS-00403: expression 'PURCHASE_NEW' cannot be used as an INTO-target of a SELECT/FETCH statement
Error(9,19): PL/SQL: ORA-00904: : invalid identifier
Thank you very much.非常感谢。
You insert rows into a table with INSERT
.使用
INSERT
将行插入到表中。 You access the row's new values with :NEW
.您可以使用
:NEW
访问该行的新值。 If you want to know whether the new quantity_stock
amount is zero, check :new.quantity_stock = 0
.如果您想知道新的
quantity_stock
数量是否为零,请检查:new.quantity_stock = 0
。 In the trigger's WHEN
clause the NEW
must have no preceding colon, however.然而,在触发器的
WHEN
子句中, NEW
前面不能有冒号。
CREATE OR REPLACE TRIGGER orders_after_update
AFTER UPDATE OF quantity_stock ON inventory
FOR EACH ROW
WHEN (new.quantity_stock = 0)
BEGIN
INSERT INTO purchase_new (product_id, store_id) VALUES (:new.product_id, :new.store_id);
END;
I'm not sure why you are checking new and old quantity_stock
values;我不知道你为什么要检查新的和旧的
quantity_stock
值; code you wrote will make trigger fire only when you update that value to what it was, because only then will their difference be zero.只有当您将该值更新为原来的值时,您编写的代码才会触发触发器,因为只有这样它们的差异才会为零。 But, I hope you know what you're doing.
但是,我希望你知道你在做什么。
Sample data:样本数据:
SQL> create table inventory (product_id number, store_id number, quantity_stock number);
Table created.
SQL> create table purchase_new (product_id number, store_id number);
Table created.
SQL> insert into inventory values (100, 1, 20);
1 row created.
SQL> insert into inventory values (200, 2, 50);
1 row created.
SQL>
SQL> create or replace trigger orders_after_update
2 after update of quantity_stock
3 on inventory
4 for each row
5 when (new.quantity_stock - old.quantity_stock = 0)
6 begin
7 insert into purchase_new (product_id, store_id)
8 values (:new.product_id, :new.store_id);
9 end;
10 /
Trigger created.
Nothing will happen because old and new values aren't the same:什么都不会发生,因为旧值和新值不一样:
SQL> update inventory set quantity_stock = 15 where product_id = 100;
1 row updated.
SQL> select * From inventory;
PRODUCT_ID STORE_ID QUANTITY_STOCK
---------- ---------- --------------
100 1 15
200 2 50
SQL> select * From purchase_new;
no rows selected
But, if they are equal, then trigger will do something:但是,如果它们相等,那么 trigger 会做一些事情:
SQL> update inventory set quantity_stock = 15 where product_id = 100;
1 row updated.
SQL> select * From inventory;
PRODUCT_ID STORE_ID QUANTITY_STOCK
---------- ---------- --------------
100 1 15
200 2 50
SQL> select * From purchase_new;
PRODUCT_ID STORE_ID
---------- ----------
100 1
SQL>
If trigger line #5 was如果触发线 #5 是
when (new.quantity_stock = 0)
then it would make more sense (to me, at least).那么它会更有意义(至少对我来说)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.