views:

125

answers:

2

I would like to do something in Java (using iBatis, JDBC, etc., really in SQL) like:

SELECT SUM(rowName) FROM myTable WHERE id = [myArrayOfIds]

Where myArrayOfIds can be almost any length. Now I know you can do:

SELECT SUM(rowName) FROM myTable WHERE id IN (x, y, z)

but what happens for longer lists? For example my list could be as little as a few items to hundreds or more items. How can I do this?

+1  A: 

one alternative would be to insert those ids to a table, then do a join

SELECT SUM(rowName) FROM myTable ta inner join tempTable tb on ta.id = tb.id
BlackTigerX
it's still the same problem, you've just moved the array (IN) to the insert command.
Stephane Grenier
you can't do inserts using "in", but you can build a batch of statements within a transaction
BlackTigerX
I'd say, test first to see if the performance is actually bad, I have done similar select statements using "in" and the performance is good enough that I don't have to try other alternatives
BlackTigerX
batching is great if you can do it at set times, but as part of a normal day to day usage, you wouldn't want to do that on a system. Generally when you're doing updates it's based on user interaction, to which I wouldn't recommend the use of batching...
Stephane Grenier
+1  A: 

I think it depends on your flavour of SQL. For instance, Oracle does not allow more than 1000 values in an IN() list. Other flavours may vary.

APC