【发布时间】:2023-03-23 21:47:01
【问题描述】:
由于某种原因,我很难完全理解触发器。对于我的家庭作业,我需要创建一个表,其中包含每个产品的产品 ID、总销售额和总销量(这些列已经在两个不同的表中)。然后我创建一个触发器,当来自不同表的 orderplaced 列更新为 1 时更新此表。不完全确定从哪里开始。由于我创建的表是空的,我会按照分配的建议做一个 UPDATE 表还是一个 INSERT 因为列是空的?如果有人能把我引向正确的方向,我将不胜感激..
CREATE TABLE bb_sales_sum (
idProduct number(2) NOT NULL,
total number(6,2),
quantity number);
CREATE OR REPLACE TRIGGER BB_SALESUM_TRG
AFTER UPDATE OF orderplaced on bb_basket
FOR EACH ROW
WHEN (NEW.orderplaced = 1)
DECLARE
lv_count Number;
BEGIN
if :new.orderplaced = 1 then
for item in
(select idproduct, (quantity * price) AS total, quantity
from bb_basketitem
where idbasket = :old.idbasket)
loop
select count(*)
into lv_count
from bb_sales_sum where idProduct = item.idproduct;
if lv_count = NULL then
INSERT INTO bb_sales_sum
VALUES (item.idproduct, item.total, item.quantity);
else
update bb_sales_sum
set quantity = item.quantity where
idProduct = item.idproduct;
end if;
end loop;
end if;
END;
/
【问题讨论】:
标签: oracle plsql database-trigger