I'm trying to use arcpy.da.SearchCursor to go through records in a feature class stored in a SQL SDE database. I do so through a feature layer (created in arcpy) called "unit_lyr". When I run the da.SearchCursor, the where clause is ignored and arcpy returns all records in the layer. When I run it using the old arcpy.SearchCursor, everything runs as expected.

Any idea why?

# Run it the old way, everything goes fine.
unit_list = []
rows = arcpy.SearchCursor('unit_lyr', 'UnitDate IS NOT NULL')
for row in rows:
    if row.UnitName is not None:
        if len(row.UnitName) > 1:

>>> len(unit_list)

# Use arcpy.da and all values are returned.
unit_list = []
with arcpy.da.SearchCursor('unit_lyr', 'UnitName', 'UnitDate IS NOT NULL') as rows:
    for row in rows:
        if row[0] is not None:
            if len(row[0]) > 1:

>>> len(unit_list)
  • I have had this happen randomly before too. Seems like there could be some kind of bug. what is even more strange is even your if row[0] is not None: didn't properly filter the results either. – crmackey Apr 2 '15 at 21:07
  • What happens if you skip the layer and run the cursors directly on the data? – DWynne Apr 3 '15 at 0:17
  • 1
    I think you should show the precise code that you are running without the >>> prompts so that we can be sure of your indentation etc and can copy/paste for testing. – PolyGeo Apr 3 '15 at 0:20
  • @PolyGeo: removed the interpreter arrows so code can be copy/pasted. It's not an indentation error though; there's no syntax error that arises. – Mike Davlantes Apr 6 '15 at 14:39
  • @DWynne: It runs fine when I run the cursors directly on the data. This is certainly inconvenient given what I'm trying to accomplish, but if running the old fashioned arcpy.SearchCursor is too slow I might end up going directly to the data. – Mike Davlantes Apr 6 '15 at 14:56

If your current version is 10.1 (as one of your tags suggests), there is a bug for da searchcursor, see this bug:

NIM076948 The arcpy.da.SearchCursor() where clause does not work with a table view.

I think it also applies to feature layers. Issues addressed at 10.2 note this bug, see this:


Not sure if it works, but you could try a selection query (Select Layer by Attributes) on the orig source and use that in the cursor...or, alternately, write the feature layer to scratch or temp workspace and apply the where clause there.

  • Thanks. I was looking for a bug report but couldn't find anything about it. I am using 10.1, so I believe this is my problem. Now I just have to go back through my old code in other projects and see if this has been causing problems I haven't even noticed. – Mike Davlantes Apr 6 '15 at 15:00

I do believe your for-loop is not running using your with statement. The picture reflects this by the absence of an indentation on your for-loop. Hope that helps.


  • 2
    That's just showing how it would look at a prompt. The indentation is legit. – DWynne Apr 3 '15 at 0:04
  • 1
    If the indentation were incorrect, the OP would be getting a syntax error. – nmpeterson Apr 3 '15 at 4:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.