简体   繁体   English

PL SQL触发器当插入一个表插入另一个表

[英]PL SQL trigger When Insert a table insert into another Table

I attached two table below I need a trigger when insert do_chd table in same time insert in do_chd_hist table And DO_MST table to DO_MST_HIST TAbLE我在下面附上了两个表 我需要一个触发器,当插入 do_chd 表同时插入 do_chd_hist 表和 DO_MST 表到 DO_MST_HIST TAbLE

create table do_mst
 ( fdo_no varchar2(14) not null,
 fdo_date date not null,
 code_db varchar2(11),
 db_name varchar2(25),
 code_sdb varchar2(11),
 sdb_name varchar2(25),
 code_sst varchar2(11),
 sst_name varchar2(25),
 constraint fdo_no_pk primary key(fdo_no)
 );

create table do_mst_hist
 ( fdo_no varchar2(14) not null,
 fdo_date date not null,
 code_db varchar2(11),
 db_name varchar2(25),
 code_sdb varchar2(11),
 sdb_name varchar2(25),
 code_sst varchar2(11),
 sst_name varchar2(25),
 constraint fdo_no_pk primary key(fdo_no)
 );

create table do_chd
 ( fdo_no varchar2(14) not null,
 itemcode varchar2(11) not null,
 name varchar(27) not null,
 unit_price varchar2(11),
 req_qty number(11) not null,
 total_price number(14),
 unit varchar2(7),
 constraint fdo_no_fk foreign key(fdo_no)
 references do_mst(fdo_no)
 );

create table do_chd_hist
 ( fdo_no varchar2(14) not null,
 itemcode varchar2(11) not null,
 name varchar(27) not null,
 unit_price varchar2(11),
 req_qty number(11) not null,
 total_price number(14),
 unit varchar2(7),
 constraint fdo_no_fk foreign key(fdo_no)
 references do_mst(fdo_no)
 );

Can anybody help me to write this trigger?有人可以帮我写这个触发器吗?

While I agree with what being said in the comments, I don't think there's a reason to think twice about writing up a create trigger syntax, and I'm happy to show you the solution to your problem:虽然我同意评论中所说的,但我认为没有理由在编写 create trigger 语法时三思而后行,我很高兴向您展示问题的解决方案:

CREATE OR REPLACE TRIGGER do_chd_insert_trg
  AFTER INSERT do_chd
  FOR EACH ROW
DECLARE
    sal_diff number;
BEGIN
    insert into do_chd_hist
    values (new.fdo_no, new.itemcode, new.name, new.unit_price, new.req_try, new.total_price, new.unit);
END;
/

You can use this syntax to generate the trigger to the second table, after changing the respectable column names.在更改相应的列名称后,您可以使用此语法生成第二个表的触发器。 You can read more about oracle trigger syntax here - https://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm您可以在此处阅读有关 oracle 触发器语法的更多信息 - https://docs.oracle.com/cd/B19306_01/appdev.102/b14251/adfns_triggers.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM