0

Event テーブルの totalPayment を計算するトリガーを作成したいと考えています。しかし、totalPayment 列を計算するには、Location と Equipment という別の 2 つのテーブルを参照します。以下は、テーブルのテーブル構造です。

create table equipment(
equipmentID varchar2(10) primary key,
equipmentName varchar2(50) not null,
equipmentPriceUnit decimal(18,2) not null
);

create table location(
locationID varchar2(10) primary key,
locationName varchar2(50) not null,
locationMaxCapacity int not null,
locationPriceHour decimal(18,2) not null
);

create table event(
eventID varchar2(10) primary key,
eventName varchar2(50) not null,
eventDurationHour int not null,
noAudience int not null,
equipmentID references equipment(equipmentID) null,
equipmentQuantity int null,
locationID references location(locationID) null,
**totalPayment decimal(18,2) null**
);

この計算を行う式は

totallocation := locationPriceHour*eventDurationHour;
totalequipment := equipmentPriceUnit*equipmentQuantity;
totalPayment :=totallocation + totalequipment;
4

1 に答える 1

0

これを試して:

CREATE OR REPLACE TRIGGER trg_event_update
AFTER UPDATE ON event
FOR EACH ROW
REFERENCING new AS new old AS old
DECLARE
  totalLocation location.locationPriceHour%TYPE;
  totalequipment equipment.equipmentPriceUnit%TYPE;
  ... -- declaration variables
BEGIN
  SELECT l.locationPriceHour * :new.eventDurationHour
    INTO totalLocation
    FROM location
   WHERE locationID = :new.locationID;

  SELECT e.equipmentPriceUnit * :new.equipmentQuantity
    INTO totalequipment
    FROM equipment
   WHERE equipmentID = :new.equipmentID;

  :new.totalPayment := totalLocation + totalequipment;

END;
/

計算フィールドは使用しないことをお勧めします。代わりに、次のように実行時に値を計算します。

SELECT (SELECT l.locationPriceHour
          FROM location l
         WHERE l.locationID = ev.locationID) * ev.eventDurationHour
       + (SELECT eq.equipmentPriceUnit
            FROM equipment eq
           WHERE eq.equipmentID = ev.equipmentID) * ev.equipmentQuantity
       AS totalPayment
  FROM event ev;
于 2013-04-02T07:37:12.417 に答える