?
Solved

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

Posted on 2004-10-15
7
Medium Priority
?
2,153 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
7 Comments
 
LVL 23

Accepted Solution

by:
seazodiac earned 136 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 132 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 35

Assisted Solution

by:Mark Geerlings
Mark Geerlings earned 132 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had to make fundamental changes to a table in Oracle, but haven't been able to get any downtime?  I'm talking things like: * Dropping columns * Shrinking allocated space * Removing chained blocks and restoring the PCTFREE * Re-or…
From implementing a password expiration date, to datatype conversions and file export options, these are some useful settings I've found in Jasper Server.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

718 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