Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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

Oracle definitely allows more than 1000 items in the IN clause. It's your persistence tool that is limiting this. iBatis or Hibernate, whatever. Use Oracle Sqlplus and you'll see this is not an Oracle limit.

Suggestion from BlackTigerX would work, or you could call the query multiple times, passing 1000 items at a time and aggregating the results. Either way, you're just working around your persistence tool limitation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top