如果系统达到特定日期,我怎么能有一个更新某个字段的触发器?
即。
+---------------------+ +-------------+
| Trains | | Trips |
+---------------------+ +-------------+
| id | | id |
| distanceTraveled | | endDate |
| | | trainUsed |
| | | distance |
+---------------------+ +-------------+
火车:
旅行:
执行:
2天内的最终结果将是
火车:
答案 0 :(得分:7)
答案 1 :(得分:3)
为了充实@ Mat的答案,我认为你想要的是这样的:
create or replace procedure update_train_distance(p_train_id trains.id%type,
p_distance trips.distance%type) is
begin
update trains
set distancetraveled = nvl(distancetraveled,0)+p_distance
where id = p_train_id;
end update_train_distance;
begin
dbms_scheduler.create_program('sched_train_update',
'STORED_PROCEDURE',
'UPDATE_TRAIN_DISTANCE',
2,
TRUE);
dbms_scheduler.define_program_argument('sched_train_update',
1,
'p_train_id',
'VARCHAR2',
'0');
dbms_scheduler.define_program_argument('sched_train_update',
2,
'p_distance',
'NUMBER',
0);
end;
create or replace trigger trips_sched_ai
after insert on trips
for each row
begin
dbms_scheduler.create_job(job_name => 'TRIP_' || :new.id,
program_name => 'sched_train_update',
start_date => :new.enddate,
auto_drop => true);
dbms_scheduler.set_job_argument_value(job_name => 'TRIP_' || :new.id,
argument_name => 'p_train_id',
argument_value => :new.trainid);
dbms_scheduler.set_job_argument_value(job_name => 'TRIP_' || :new.id,
argument_name => 'p_distance',
argument_value => :new.distance);
dbms_scheduler.enable('TRIP_' || :new.id);
end trg_trips_sched;
你应该记住,这是一个未经考验的例子,可能有一些我错过的东西。至少,您可能需要添加触发器以在执行作业之前处理更新或删除。