java.sql.SQLSyntaxErrorException:ORA-01795:列表中的最大表达式数为1000

时间:2014-08-02 16:54:05

标签: java arrays angularjs java-ee oracle-sqldeveloper

我们都知道,当尝试使用超过1000个值的bomabard查询时,我们会看到此异常。列限制的最大值是1000 ..最好的解决方案是将查询拆分为两个。你可以建议一些可能的代码重构方法,以使我的问题消失。任何帮助将不胜感激。

audTypeFieldIdList 大于1000个值时,我们会看到异常。

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;

1 个答案:

答案 0 :(得分:5)

IN子句中不能使用超过1000个条目。有两种解决方案如下所述:

  1. 使用inner query解决此问题。您可以创建一个临时表,并在IN子句中使用它。

  2. 使用由IN子句分隔的多个OR子句在1000个条目的批处理中打破它。

  3. 示例查询:

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

    Read more..并查看Oracle FAQ