Select by attribute SQL statement

4955
4
01-20-2015 09:13 AM
ThomasCaruso
New Contributor III

Hi folks,

I'm having an issue with an SQL statement for the select layer by attribute function and I can't figure out what's going on, because it worked perfectly on a smaller dataset, but when I try to run it on a larger selection of data it fails and gives me an error. Here is the offending piece of code:

for key in matches:
        arcpy.SelectLayerByAttribute_management(wellsFL, "NEW_SELECTION", "U_ID = {}".format(key))
        select_id = tuple(matches[key])
        arcpy.SelectLayerByAttribute_management(orpsFL, "NEW_SELECTION", "U_ID IN {}".format(select_id))

the issue is line # 4. "matches" is a dictionary that stores a well ID with a list of ID's of potential orps matches. I want line 4 to select all the orps that have U_ID's in a particular entry of the dictionary.

Like I said, when testing this on a smaller dataset, it worked perfectly fine. But now that I try it on a full town's worth of wells/orps, it fails, giving the following error code:

Traceback (most recent call last):

  File "<pyshell#8>", line 1, in <module>

    arcpy.SelectLayerByAttribute_management(orpsFL, "NEW_SELECTION", "U_ID = {}".format(select_id))

  File "C:\Program Files (x86)\ArcGIS\Desktop10.2\arcpy\arcpy\management.py", line 6494, in SelectLayerByAttribute

    raise e

ExecuteError: ERROR 000358: Invalid expression

Failed to execute (SelectLayerByAttribute).

What is suddenly making this expression invalid??

Thanks in advance,

Tom

0 Kudos
4 Replies
ThomasCaruso
New Contributor III

Hi Sol,

Thanks for your response, however I just figured out that the function came across an empty tuple and then crashed. Apparently select by attribute can't deal with an empty tuple in the argument, I assumed it would just skip it. Oops.

Have a great day!

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

It isn't an issue with the ArcGIS tool.  The tool is passing through part of an SQL expression the user generates.  In this case, the tool is just passing along an error message from other code that is saying an invalid SQL expression was used.

0 Kudos
JamesCrandall
MVP Frequent Contributor

I just figured out that the function came across an empty tuple and then crashed. Apparently select by attribute can't deal with an empty tuple in the argument, I assumed it would just skip it. Oops.

This is likely not going to be your only issue.  Depending upon what RDBMS you are working with will also have an affect on your "IN" clause because each one has their own limits to the number of values allowed in the SQL statement.

One solution is to cycle thru a pre-set number of values (say up to 1000 "U_ID" values attempting to be passed into the sql statement).  If it exceeds that limit then use an alternate "arcpy.SelectLayerByAttribute_management" with the "ADD_TO_SELECTION" property assigned, if less than the 1000 limit then just keep the "NEW_SELECTION" property assigned.

Another option is to create in_memory table of the selected "U_ID" values, join this back to your feature class and then select on values NOT NULL in the join fields.

Sorry no code to post up.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

It could be a SQL Syntax issue.  The SQL "IN" operator requires the set of values to be enclosed in parentheses, e.g., expression IN (value1, value2, value3, ...)

0 Kudos