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
Solved

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

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Converting a row into a column 2 60
passing parameters to sql script oracle 4 58
exp/imp 25 84
Oracle 12c Default Isolation Level 17 41
Cursors in Oracle: A cursor is used to process individual rows returned by database system for a query. In oracle every SQL statement executed by the oracle server has a private area. This area contains information about the SQL statement and theā€¦
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 take different types of Oracle backups using RMAN.
This video shows how to copy an entire tablespace from one database to another database using Transportable Tablespace functionality.

790 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