Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes JDBC and the fly likes multiple inserts with @@identity Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "multiple inserts with @@identity" Watch "multiple inserts with @@identity" New topic
Author

multiple inserts with @@identity

jdbc stretch
Greenhorn

Joined: Jul 02, 2003
Posts: 1
help! help, someone!

here's the problem
mssqlserver.jar (jdbc2.0 compat)
t1 is auto-increment primary-key.
part of t2's primary key is the @@identity of t1
i want to say:
PreparedStatement("
insert into t1 (a,b,c,d) values (?,?,?,?); insert into t2 (e,f,g) values (@@identity,?,?)
")
when i run through and set ?1,?2,?3,?4 i'm ok
when i set ?6,?7 and ask jdbc to do this for me, it bombs out with InvalidParameterBinding.
any thoughts? is this just an impossible task?
****the catch here is that i need to be using addBatch on insert (big, big insert batch) and i don't wanna do the logical equivalent of (insert-into-t1, select identity, insert-into-t2) for each row. this will not meet performance reqs
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
You're not preparing a statement. You're trying to prepare two statements at once. That won't work. Since you're adding them to a batch anyway -- have you tried preparing them as separate statements
insert into t1 (a,b,c,d) values (?,?,?,?)
insert into t2 (e,f,g) values (@@identity,?,?)
And sticking them into your batch, in that order? I don't see why that shouldn't work...
- Peter
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: multiple inserts with @@identity
 
Similar Threads
Temporary objects
[ DAO ] A question
Sql query problem
sql syntax question
copying an object