Solved

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

Posted on 2004-10-15
7
2,128 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 35

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting TNS:Connect timeout occurred while opening the application 5 76
Get the parent node - XMLTYPE 9 69
How to free up undo space? 3 39
Help on model clause 5 27
Article by: Swadhin
From the Oracle SQL Reference (http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/queries006.htm) we are told that a join is a query that combines rows from two or more tables, views, or materialized views. This article provides a glimps…
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

816 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

8 Experts available now in Live!

Get 1:1 Help Now