4

I need to get all features from feature class where their field value matches to input array. To achive this one may set IQueryFilter.WhereClause property similar to 'FIELD_NAME IN (array[0], array[1], array[2],...., array[n-1])'. But the size of array might be quite big.

The questions are how long IN statement can be? And can I define it at runtime?

Thanks in advance!

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Eduard Lepner
  • 213
  • 2
  • 6

3 Answers3

7

The exact number of explicit terms in an IN clause varies by RDBMS. Oracle limits you to 1000 explicit terms, while the SQL-Server doc only mentions "several thousands".

The real issue is that an IN clause is not very efficient. If you have more than a few hundred terms, you'd likely get a faster query by loading a temporary table with the values, then doing a join (or an IN to a subselect, which is implemented as a join). In ArcGIS, selection sets can be used for this purpose (vice explicitly creating a temporary table, but the implementation is the same).

Vince
  • 20,017
  • 15
  • 45
  • 64
2

I didn't find a way how to find out exact size of IN statement and then safaly build it but I have found a workaround. I have reduced described problem to search the rows satisfying the following where clause: (OBJECTID IN (123, 456,789)) AND FIELD1 = x AND FIELD2 = y AND .... I.e. the statement has IN statement with very big list of numbers and arbitrary AND statements.

Here is the workaround:

  1. I used ITable.Select to create empty selection set.
  2. Then populate this set with ISelectionSet.AddArray with necessary OIDs. It allows to avoid from using IN statement like in WHERE clause OBJECTID IN (123, 456, 789)
  3. Apply additional additional where caluses to selection set ISelectionSet.SearchSearch(IQueryFilter, bool, out ICursor).
Eduard Lepner
  • 213
  • 2
  • 6
0

How can the below be replaced to use a variable in place of hard code values filter.WhereClause = featureClass.OIDFieldName + " IN (1,2,3,4,5,6)";

string strFIDS="1,2,3,4,5,6";

Does the below work ? filter.WhereClause = featureClass.OIDFieldName + " IN (strFIDS)";

addcolor
  • 1,282
  • 9
  • 19