2

we all know we see this exception when try to bomabard the query with more than 1000 values. the maximum for the column limit is 1000.. the best possible solution is to split the query into two.guys can u suggest some possible ways of code refactoring to make my problem go away.any help would be appreciated.

we see the exception when audTypeFieldIdList are greater than 1000 values.

try {
        String query = "select AUDIT_TYPE_FIELD_ID, FIELD_NAME from AUDIT_TYPE_FIELD where AUDIT_TYPE_FIELD_ID in (";
        int x = 0;
        for (int y = 1; y <= audTypeFieldIdList.size(); y++) {
            query += audTypeFieldIdList.get(x);
            if (y != audTypeFieldIdList.size()) {
                query += ", ";
            }
            x++;
        }
        query += ")";    



List<Long> audTypeFieldIdList, Connection connection) {          

    ResultSet rs = null;
    Statement stmt = null;
    List<AuditTypeField> audTypeFieldList = new ArrayList<AuditTypeField>();
    try {
        String query = "select AUDIT_TYPE_FIELD_ID, FIELD_NAME from AUDIT_TYPE_FIELD where AUDIT_TYPE_FIELD_ID in (";
        int x = 0;
        for (int y = 1; y <= audTypeFieldIdList.size(); y++) {
            query += audTypeFieldIdList.get(x);
            if (y != audTypeFieldIdList.size()) {
                query += ", ";
            }
            x++;
        }
        query += ")";



        stmt = connection.createStatement();
        rs = stmt.executeQuery(query);
        while (rs != null && rs.next()) {
            AuditTypeField audTypeField = PluginSystem.INSTANCE
                    .getPluginInjector().getInstance(AuditTypeField.class);
            audTypeField.setId(rs.getLong("AUDIT_TYPE_FIELD_ID"));
            audTypeField.setName(rs.getString("FIELD_NAME"));
            audTypeFieldList.add(audTypeField);
        }
        return audTypeFieldList;
        return audTypeFieldList;
3
  • 1
    Where are the 1000+ elements in the list coming from? Presumably, a human isn't entering that many values. Are they coming from a different query against the database? If so, you'd want to combine the two queries. Aug 2, 2014 at 22:18
  • YES IT IS COMING FROM TWO DIFFERENT QUERYS AGAINST THE DATABASE Aug 3, 2014 at 0:04
  • Please don't shout. So why are you doing two separate queries?
    – Alex Poole
    Aug 3, 2014 at 8:42

1 Answer 1

5

You can't use more than 1000 entries in IN clause. There are 2 solutions as mentioned below:

  1. Use inner query to solve this issue. You can create a temporary table and use that in your IN clause.

  2. Break it in the batch of 1000 entries using multiple IN clause separated by OR clause.

sample query:

select * from table_name
  where
      column_name in (V1,V2,V3,...V1000)
  or
      column_name in (V1001,V1002,V1003,...V2000)
  ...

Read more.. and see Oracle FAQ

3
  • i would go for the later one since the query is dynamically generated, do you have any ideas on second part how should i go about it? Aug 2, 2014 at 16:58
  • writer a simple program based on the size of entries. It's very simple.
    – Braj
    Aug 2, 2014 at 16:59
  • follow the link that I shared you in my updated post.
    – Braj
    Aug 2, 2014 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.