panpanW
asked on
how to insert into another table when insert into this table?
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?
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?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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...)