This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes JDBC and the fly likes Using select values for inserts in a batch Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "Using select values for inserts in a batch" Watch "Using select values for inserts in a batch" New topic
Author

Using select values for inserts in a batch

mohamed zafer
Ranch Hand

Joined: Nov 03, 2000
Posts: 62
Hi There,

I want to insert into mutliple tables using batch update like this,

Statement stmt= dbCon.createStatement();
stmt.addBatch("SELECT ID_SEQ.nextval as AID from dual");
stmt.addBatch("INSERT INTO AID_N VALUES (<AID>, "1");
stmt.addBatch("INSERT INTO AID_B VALUES (<AID>, "43");
stmt.addBatch("INSERT INTO AID_C VALUES (<AID>, "2");

How can I use the value AID selected in the first query, in the subsequent queries [inplace of <AID>].

Thanks
Mohamed Zafer
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You could just put the sequence select into your insert:


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Gagan Gulati
Greenhorn

Joined: Jul 27, 2004
Posts: 7
Not really.
Because if you do that, 3 nextvals would come from the sequence.

Instead you could get the result of the select query first and then add the three Inserts in the batch
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

I was assuming <AID> was the PK (which is the normal reason for using sequences). Reading the question again, I see that perhaps this isn't how mohamed zafer want's to use it. In which case you are correct, you need to do it in two seperate Statements.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using select values for inserts in a batch
 
Similar Threads
executing an INSERT statement using a PreparedStatement
Transaction error(Invalid cursor state)
Batch Insert
Savepoint
Batch updates of preparedstatement