ORA-01795: 列表中的最大表達式數為 1000


今天查看日志的時候發現多次出現如下的異常,查閱了資料后發現IN語句中寫的表達式的最大數量不能超過1000。

ORA-01795: 列表中的最大表達式數為 1000

  1. 00000 - "maximum number of expressions in a list is 1000"
    *Cause: Number of expressions in the query exceeded than 1000.
    Note that unused column/expressions are also counted Maximum number of expressions that are allowed are 1000.
    *Action: Reduce the number of expressions in the list and resubmit.
    行 22 列 2,586 出錯

查看代碼后發現有一個意思大概如下的代碼。

public class InTest {
	@Test
	public void test() throws SQLException {
		OracleDBUtil util = new OracleDBUtil();
		Connection conn = util.connection();
		Statement st = conn.createStatement();
		String instr = "";
		for (int i = 0; i < 2000; i++) {
			instr += "," + i;
		}
		if (instr.length() > 1) {
			instr = instr.substring(1);
		}
		if (instr.length() > 0) {
			String sql = "select userid from LOG where id in(" + instr + ")";
			System.out.println(sql);
			ResultSet rs = st.executeQuery(sql);
			while (rs.next()) {
				System.out.println(rs.getString("userid"));
			}
		}
		util.close(conn);
	}
}

解決方法就是拆分IN里面的條件,將表達式的數量控制在1000以內,然后通過OR語句連接。

(field in(s1,s2,s3.....s999)) or (field  in (s1,s2,s3....s999))

另一種解決方法就是作為子查詢。

select userid from LOG where id in( select id from LOG )

oracle in條件

An in_condition is a membership condition. It tests a value for membership in a list of values or subquery。

If you use the upper form of the in_condition condition (with a single expression to the left of the operator), then you must use the upper form of expression_list. If you use the lower form of this condition (with multiple expressions to the left of the operator), then you must use the lower form of expression_list, and the expressions in each expression_list must match in number and data type the expressions to the left of the operator. You can specify up to 1000 expressions in expression_list.Oracle Database does not always evaluate the expressions in an expression_list in the order in which they appear in the IN list. However, expressions in the select list of a subquery are evaluated in their specified order.

參考

6.14 IN Condition


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM