简体   繁体   English

Oracle触发器插入其他表

[英]Oracle trigger insert other table

I have 2 tables which are my_school and my_class 我有2个表,分别是my_schoolmy_class

And " my_school " table has ' info_id ' column and also " my_class " table has ' info_id ' then I want to get a query that automatically generate " info_id " then I found solution.. 并且“ my_school ”表具有“ info_id ”列,并且“ my_class ”表也具有“ info_id ”,那么我想获得一个自动生成“ info_id ”的查询,然后我找到了解决方案。

Here are my working TRIGGER on " my_school " table... 这是我在“ my_school ”表上工作的触发器...


CREATE OR REPLACE TRIGGER info_id

 before insert on my_direction 

 for each row

 begin 

 if :NEW.WAY_ID is null then 

 :NEW.WAY_ID := example_id_seq.nextval; 

 end if;

 end;

It works and it's generating auto id when inserting value. 它可以工作,并且在插入值时会生成自动ID。 But now how to get this trigger do it on "my_class" table when users insert value on my_school's table then take id with "my_class" table's "info_id" column same time? 但是现在,当用户在my_school的表上插入值,然后同时使用“ my_class”表的“ info_id”列获取ID时,如何在“ my_class”表上执行此触发器?

You can create trigger on my_school table to update info_id similar to that you have explained and while inserting records, use returning into clause. 您可以在my_school表上创建触发器以更新info_id与您所解释的类似),并在插入记录时使用info_id子句。

Declare a variable to store returned value, for example 声明一个变量以存储返回值,例如

v_info_id number(9);

And use it in returning into clause 并将其用于返回条款

insert into my_school(column.......list) 
values (values........list)
RETURNING info_id INTO v_info_id;

Use v_info_id in your program to insert value of info_id into another tables. 在程序中使用v_info_idinfo_id值插入另一个表。

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

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