我希望创建一个触发器,在父表中输入一条记录后,在子表中插入7条记录。 子项必须包含父表的主键。
我试过这个
CREATE TRIGGER journey_date_append
AFTER INSERT
ON BUS_DETAILS
FOR EACH ROW
DECLARE
BEGIN
insert into BUS_JOURNEY_DETAILS values (BUS_JOURNEY_DETAILS_SEQ.nextval, new.BUS_DETAILS_PK,sysdate, 'subham',sysdate, 'subham', 'NORMAL')
END;/
它被保存但有一些错误,当我运行它时,
Row 15: ORA-04098: trigger 'FRS2014_OVMGMT.ADD_JOURNEY_DATE' is invalid and failed re-validation
插入语句是正确的,我已单独检查过。
答案 0 :(得分:1)
您只显示了一个插入的子记录,您需要show errors
或select * from user_errors
才能看到实际问题。一个明显的问题是,您没有使用冒号为new
pseudorecord值加前缀 - 它应该是:new.BUS_DETAILS_PK
。并且您没有声明任何变量,因此您不需要DECLARE
关键字:
CREATE TRIGGER journey_date_append
AFTER INSERT
ON BUS_DETAILS
FOR EACH ROW
BEGIN
insert into BUS_JOURNEY_DETAILS values (BUS_JOURNEY_DETAILS_SEQ.nextval,
:new.BUS_DETAILS_PK,sysdate, 'subham',sysdate, 'subham', 'NORMAL')
END;
/
最好列出目标表列,以便确保值正确对齐,并为将来的架构更改提供一些保护。只有您知道您传递的其他值是否有效,并且当前表格的顺序是否正确。
答案 1 :(得分:0)
create or replace trigger PARENT_TRG_AI_R
after insert on PARENT
for each row
begin
insert into child (column1,column2,...,PARENT_ID) values (value1,value2,...,:new.PARENT_ID);
insert into child (column1,column2,...,PARENT_ID) values (value1,value2,...,:new.PARENT_ID);
insert into child (column1,column2,...,PARENT_ID) values (value1,value2,...,:new.PARENT_ID);
insert into child (column1,column2,...,PARENT_ID) values (value1,value2,...,:new.PARENT_ID);
insert into child (column1,column2,...,PARENT_ID) values (value1,value2,...,:new.PARENT_ID);
insert into child (column1,column2,...,PARENT_ID) values (value1,value2,...,:new.PARENT_ID);
insert into child (column1,column2,...,PARENT_ID) values (value1,value2,...,:new.PARENT_ID);
end;
你可以随意调用触发器 - 这只是我的建议。
答案 2 :(得分:0)
要回答我自己的问题,这是在子表中添加7条记录的最终触发器 (BUS_JOURNEY_DETAILS)在父母(BUS_DETAILS)中插入记录时
CREATE OR REPLACE TRIGGER journey_date_append
AFTER INSERT
ON BUS_DETAILS
FOR EACH ROW
BEGIN
insert into BUS_JOURNEY_DETAILS values (BUS_JOURNEY_DETAILS_SEQ.nextval, :new.BUS_DETAILS_PK,sysdate, 'subham',sysdate, 'subham',sysdate, 'NORMAL');
insert into BUS_JOURNEY_DETAILS values (BUS_JOURNEY_DETAILS_SEQ.nextval, :new.BUS_DETAILS_PK,sysdate+1, 'subham',sysdate, 'subham',sysdate, 'NORMAL');
insert into BUS_JOURNEY_DETAILS values (BUS_JOURNEY_DETAILS_SEQ.nextval, :new.BUS_DETAILS_PK,sysdate+2, 'subham',sysdate, 'subham',sysdate, 'NORMAL');
insert into BUS_JOURNEY_DETAILS values (BUS_JOURNEY_DETAILS_SEQ.nextval, :new.BUS_DETAILS_PK,sysdate+3, 'subham',sysdate, 'subham',sysdate, 'NORMAL');
insert into BUS_JOURNEY_DETAILS values (BUS_JOURNEY_DETAILS_SEQ.nextval, :new.BUS_DETAILS_PK,sysdate+4, 'subham',sysdate, 'subham',sysdate, 'NORMAL');
insert into BUS_JOURNEY_DETAILS values (BUS_JOURNEY_DETAILS_SEQ.nextval, :new.BUS_DETAILS_PK,sysdate+5, 'subham',sysdate, 'subham',sysdate, 'NORMAL');
insert into BUS_JOURNEY_DETAILS values (BUS_JOURNEY_DETAILS_SEQ.nextval, :new.BUS_DETAILS_PK,sysdate+6, 'subham',sysdate, 'subham',sysdate, 'NORMAL');
END;