Oracle SQL错误:最大表达式数

时间:2017-07-13 08:14:16

标签: sql oracle

关于在Oracle SQL中出现错误的问题,请你帮我解决一下

  

ORA-01795列表中的最大表达式数为1000

我传递的价值就像

and test in (1, 2, 3.....1000)

2 个答案:

答案 0 :(得分:0)

尝试将查询拆分为多个in子句,如下所示

SELECT *
  FROM table_name
 WHERE test IN (1,2,3,....500) 
     OR test IN (501, 502, ......1000);

答案 1 :(得分:0)

您可以尝试解决方法:

将单个in分成几个:

   select ...
     from ...
    where test in (1, ...,  999) or 
          test in (1000, ..., 1999) or
          ...  
          test in (9000, ..., 9999)

将值放入(临时?)表中,比如TestTable

   select ...
     from ...
    where test in (select TestField 
                     from TestTable)

编辑:正如我所看到的,主要的难点是构建这样的查询。我们在 C#中实现它。我们收到了id s:

的集合
// Test case ids are in [1..43] range 
IEnumerable<int> Ids = Enumerable.Range(1, 43);

// Test case: 7, in actual Oracle query you, probably set it to 100 or 1000
int chunkSize = 7;

string fieldName = "test";

string filterText = string.Join(" or " + Environment.NewLine, Ids
  .Select((value, index) => new {
    value = value,
    index = index
  })
  .GroupBy(item => item.index / chunkSize)
  .Select(chunk => 
     $"{fieldName} in ({string.Join(", ", chunk.Select(item => item.value))})"));

if (!string.IsNullOrEmpty(filterText))
  filterText = $"and \r\n({filterText})";

string sql =
  $@"select MyField
       from MyTable
      where (1 = 1) {filterText}"; 

测试:

Console.Write(sql);

结果:

select MyField
             from MyTable
            where (1 = 1) and 
(test in (1, 2, 3, 4, 5, 6, 7) or 
test in (8, 9, 10, 11, 12, 13, 14) or 
test in (15, 16, 17, 18, 19, 20, 21) or 
test in (22, 23, 24, 25, 26, 27, 28) or 
test in (29, 30, 31, 32, 33, 34, 35) or 
test in (36, 37, 38, 39, 40, 41, 42) or 
test in (43))