在ORACLE SELECT 语句中 IN 的数据如果超过 1000,就会出错,解决方法也很简单,以C#代码为例:
1、先写一个方法,接收2个参数
参数1:接收 IN里面的数据,如:'a1','a2',...'a2000' ;
参数2:需要IN的列名;
public string GetSqlIn( string sqlParam, string columnName )
{
int width = sqlParam.IndexOf( "'", 1 ) - 1;
string temp = string.Empty;
for( int i = 0; i < sqlParam.Length; i += 1000 * ( width + 3 ) )
{
if( i + 1000 * ( width + 3 ) - 1 < sqlParam.Length )
{
temp = temp + sqlParam.Substring( i, 1000 * ( width + 3 ) - 1 )
+ ") OR " + columnName + " IN (";
}
else
{
temp = temp + sqlParam.Substring( i, sqlParam.Length - i );
}
}
return temp;
}
{
int width = sqlParam.IndexOf( "'", 1 ) - 1;
string temp = string.Empty;
for( int i = 0; i < sqlParam.Length; i += 1000 * ( width + 3 ) )
{
if( i + 1000 * ( width + 3 ) - 1 < sqlParam.Length )
{
temp = temp + sqlParam.Substring( i, 1000 * ( width + 3 ) - 1 )
+ ") OR " + columnName + " IN (";
}
else
{
temp = temp + sqlParam.Substring( i, sqlParam.Length - i );
}
}
return temp;
}
2、使用这个方法的返回值,代码如下:
System.Text.StringBuilder sql = new System.Text.StringBuilder("");
sql.Append ( " SELECT " );
sql.Append ( " T.A" );
sql.Append ( " FROM TEST T" );
sql.Append ( " WHERE 1=1 " );
if( Col.Length > 0 )
{
string sqlStr = GetSqlIn( Col, "ColName" );
sql.Append ( " AND T.Col IN ( " + sqlStr + " )" );
}
sql.Append ( " ORDER BY T.A" );
sql.Append ( " SELECT " );
sql.Append ( " T.A" );
sql.Append ( " FROM TEST T" );
sql.Append ( " WHERE 1=1 " );
if( Col.Length > 0 )
{
string sqlStr = GetSqlIn( Col, "ColName" );
sql.Append ( " AND T.Col IN ( " + sqlStr + " )" );
}
sql.Append ( " ORDER BY T.A" );
3、运行后得到的SQL字符串格式为:
select t.* from TEST t where t.A in (59,60) or t.A in (61,62)
这样就解决了 IN大于1000的问题。