[英]pl sql trigger total
我想創建一個觸發器來計算插入到SALESORDERDETAIL中的每一行的小計。 小計是數量*價格。 有人可以幫我了解如何解決嗎?
我相信所有必要的信息都在SALESORDERDETAIL中。 我之前已將觸發器與序列結合使用,但未將觸發器用於其他任何事情。 我每次都需要一個程序和執行程序嗎? 我該怎么寫? 感謝您的幫助,因為我在最后一天一直堅持不懈。 謝謝!
drop table SALESORDERDETAIL;
create table SALESORDERDETAIL (
SONo CHAR(9), -- sales order number
ItemID char(8), -- item being ordered (finished goods)
SOquantity number(5), -- quantity of the item being ordered
Price Number(10,2), -- unit price of each item
subtotal Number(10,2), -- sales order detail (line) subtotal
constraint SOD_pk primary key(SONo,ItemID),
constraint SOD_FG_FK foreign key(ItemID) references FinishedGoods(itemid),
constraint SOD_SO_FK foreign key(SONo) references SalesOrders(SONo)
);
Insert into SALESORDERDETAIL (SONO,ITEMID,SOQUANTITY,PRICE,SUBTOTAL) values
('SO1000001','FG000001',100,10,'');
Insert into SALESORDERDETAIL (SONO,ITEMID,SOQUANTITY,PRICE,SUBTOTAL) values
('SO1000001','FG000002',50,2,'');
commit;
假設這是Oracle? 如果是這樣,請考慮使用虛擬列而不是觸發器:
... subtotal AS (soquantity * price), ...
...但是如果您仍然想要觸發器,它將看起來像這樣:
CREATE OR REPLACE TRIGGER SALESORDERDETAIL_before BEFORE INSERT OR UPDATE ON SALESORDERDETAIL FOR EACH ROW BEGIN :new.subtotal := :new.soquantity * :new.price; END; /
我不喜歡不太優雅的解決方案,但看起來它是用於教育而不是解決問題,祝您好運。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.