原因:这个问题出现的原因很明显,在SQL语句中in查询时超过了1000条。
解决:解决方式有两个:
1.通过截断in查询的数据,通过多个in+or的方式查询;
StringBuilder sb = new StringBuilder();
//逗号分隔的string数据转List
List<string> sellerAccountList = sellerAccounts.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
int inNum = 1; //已拼装IN条件数量
for (int i = 0; i < sellerAccountList.Count; i++)
{
if (string.IsNullOrEmpty(sellerAccountList[i])) continue;
//这里不要犯低级错误而写成:if(i == lists.length)
if (i == (sellerAccountList.Count - 1))
sb.Append(sellerAccountList[i]); //SQL拼装,最后一条不加“,”。
else if (inNum == 1000 && i > 0)
{
sb.Append(sellerAccountList[i] + ") OR SELLER_ACCOUNT IN ( "); //解决ORA-01795问题
inNum = 1;
}
else
{
sb.Append(sellerAccountList[i] + ", ");
inNum++;
}
};
2.通过与临时表建立join或exists查询(同库操作)
在Oracle新建事务结束后自动清除数据的TEMP表,在程序操作时往表中写入要in查询的数据,再通过与临时表建立join或exists查询.
注意:数据量很大时希望考虑其他查询方式,in的数值太多不管用这两种方式中的哪一个都很慢,,