Solved

how to insert into another table when insert into this table?

Posted on 2004-10-15
7
2,119 Views
Last Modified: 2008-02-01
hi experts,

I have two tables, A and B. I need to insert into a new row into table B when insert into a new row of A. How could I do that without writing two SQL in java codes?

I tried to create an insert trigger for table B, which should be triggered when a new row inserting into table A. However, I don't know how to pass the new value in the trigger body for table B since it is a variable, not a constant
for example,

insert into A (c,d,e) values (.....)

should trigger

insert into B(col1, col2) values (entry1, entry2)
but how to pass the entry1 and entry2 into the triggerB when insert happen on table A?
0
Comment
Question by:panpanW
  • 2
7 Comments
 
LVL 23

Accepted Solution

by:
seazodiac earned 34 total points
ID: 12321140
if table A and table B are totally unassociated, the values for insert to B does not have any bearings with values for insert to A,
then you's better write two sql for each.

if your concern is that you want to make sure they happen together or none
then just make sure they are in the same transaction, like putting them into one procedure

create procedure test
as
begin
insert into A (c,d,e) values (.....)
insert into B(col1, col2) values (entry1, entry2)
end;
/
0
 
LVL 11

Assisted Solution

by:cjjclifford
cjjclifford earned 33 total points
ID: 12321175

Insert trigger is the way to go. e.g.

CREATE trigger test_trig ON table_a AFTER INSERT FOR EACH ROW
BEGIN
    -- :NEW is the new row in TABLE_A...
    INSERT INTO table_b( table_a_name ) VALUES( :NEW.name );
END;
/


For insert, :NEW is the new row, for update, :OLD is the old row, :NEW the new row, for delete, :OLD is the row deleted (note AFTER action and BEFORE action have different meanings on the triggers....
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12321211
sorry, missed the bit about inserting completely non-related data into table_b....

If the data to go into table_b is not part of table_a (or cannot be calculated from the values in table_a) then there is no way (apart from creating a view to contain all relevant data, and using an INSTEAD OF trigger to insert into both tables) to get the data in in one statement - but then again, as seazodiac says, if the data is unrelated, then there should be no real reason not to use seperate SQL (transactionality can be handled by the session if you are worried about that...)
0
 
LVL 34

Assisted Solution

by:Mark Geerlings
Mark Geerlings earned 33 total points
ID: 12321611
Are the values for the insert into table B the same (or some of the same) values that are inserted into table A?  If yes, then it is easy to write a trigger to do that insert for you as cjjclifford suggested.

One possible complication: if the tables are related to each other by a foreign key, then a simple trigger approach like this will not work, it will cause a "mutating table error".  If that is true, then you have to use a more complex, multiple trigger approach, but it is possible.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
This post first appeared at Oracleinaction  (http://oracleinaction.com/undo-and-redo-in-oracle/)by Anju Garg (Myself). I  will demonstrate that undo for DML’s is stored both in undo tablespace and online redo logs. Then, we will analyze the reaso…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now