Solved

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

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction A previously published article on Experts Exchange ("Joins in Oracle", http://www.experts-exchange.com/Database/Oracle/A_8249-Joins-in-Oracle.html) makes a statement about "Oracle proprietary" joins and mixes the join syntax with gen…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
Via a live example show how to connect to RMAN, make basic configuration settings changes and then take a backup of a demo database
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.

734 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