I have an Oracle database with a table "Trains".
Each train got a JourneyID and a departureDate as foreign keys (references Journey).
A Journey is a regular trip that a train do on a regular basis (each day, only in the week, only week-ends, special period for vacations, etc...). A journey got a date of departure and a date of arrival. It also contains the dates of the period (begin and end) and a periodicity rule (a varchar, like "everyday" "every week-end", "each day of the week").
I would like to create a trigger that insert a new train, associated to the right journey, for each day in the period, depending of what the period's rule says.
I have a few problems :
How can i write such a trigger ? The trigger will... trigger once per statement, so there is no risk of triggering because of the trigger, right ?
How can i test the periode attributes where my journey foreign key match the period primary key ?
How can i do a "for each day between" with a set of date ? How to handle gaps in the days (like "not week-end" or "only week-end" conditions).
Is it only possible to do such a manipulation with SQL ?? (i assume that yes, but with a lot of subqueries inside the trigger).
Here is my try for each day in the interval :
CREATE TRIGGER Auto_Creation_Trains
BEFORE INSERT OR UPDATE ON Trajets
FOR EACH ROW
BEGIN
IF type = "Regulier - Taille Normale"
THEN
CREATE SEQUENCE temp MINVALUE 1 START WITH 1 INCREMENT BY 1 MAX VALUE 366
WHILE (temp != fin_periode - debut_periode)
LOOP
temp.nextval
INSERT INTO (trainID,Sits1,Available1,Sits1,Available2,journeyID, depart) TrainInfo(IDTrain.next, 150,150,647,647,:NEW.journeyID);
END LOOP;
DROP SEQUENCE temp
END IF;
END;
Does it seems legit ?
答案 0 :(得分:0)
我认为这个想法应该有效,当这个表获得新记录时,将许多记录插入另一个表的想法。但你放在这里的代码是有争议的。
应该是:
INSERT INTO traininfo(trainID,Sits1,Available1,Sits1,Available2,journeyID,depart)值(IDTrain.next,150,150,647,647,:NEW.journeyID)
除非这只是一个草稿而你的真实插入语句不同,否则我不明白为什么你需要在traininfo表中插入365个相同的记录。