<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: arcpy.SelectLayerByAttribute_management Issues - how to use with an update cursor. in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/arcpy-selectlayerbyattribute-management-issues-how/m-p/1397221#M70077</link>
    <description>&lt;P&gt;It is best to keep a post to a single question because asking multiple questions in the same post tends to make the comments a mess when some people will reply to one question, some to the other, and some to both.&amp;nbsp; These two questions are closely related enough one could argue it is just one question, but please keep this in mind for future questions.&lt;/P&gt;&lt;P&gt;Regarding your first question of Get Count, it is expected behavior.&amp;nbsp; As its name states, Select Layer By Attribute works on layers, not data sets.&amp;nbsp; Somewhere back a few years ago, Esri decided to augment Select Layer By Attribute to accept an input data set and it will create a layer, select on it, and return it.&amp;nbsp; This is what is happening in your case.&amp;nbsp; When you use Get Count on the data set directly, it is returning the total number of records on the data set, which is should.&lt;/P&gt;</description>
    <pubDate>Mon, 18 Mar 2024 13:33:47 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2024-03-18T13:33:47Z</dc:date>
    <item>
      <title>arcpy.SelectLayerByAttribute_management Issues - how to use with an update cursor.</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-selectlayerbyattribute-management-issues-how/m-p/1397093#M70073</link>
      <description>&lt;P&gt;Hello,&lt;BR /&gt;&lt;BR /&gt;I have two questions, firstly about using SelectByAtribute_managementm and secondly why my selection won't work with my updateCursor. I apologise for the bad formatting below.&lt;BR /&gt;&lt;BR /&gt;I am using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;arcpy.SelectLayerByAttribute_management(sTable_path, "NEW_SELECTION", sOldWhere)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;to select 2 records from a 200k+ table of records. Those which match the SQL statement within &lt;EM&gt;sOldWhere&lt;/EM&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I wish to double check the correct items are selected, I am using:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;selection_count = int(arcpy.GetCount_management(sTable_path).getOutput(0))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;This returns a count of 200k+, which is incorrect. The SQL statement is valid. It should have two items selected.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;If I save the output of the &lt;EM&gt;SelectLayerByAttribute_management&lt;/EM&gt; to a variable via:&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;itemsToRetire, itemsToRetireCount = arcpy.SelectLayerByAttribute_management(sTable_path, "NEW_SELECTION", sOldWhere)​&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can either print out&amp;nbsp;&lt;SPAN&gt;&lt;EM&gt;itemsToRetireCount&lt;/EM&gt;, or run getCount_management on&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;&lt;SPAN&gt;itemsToRetire &lt;/SPAN&gt;&lt;/EM&gt;&lt;SPAN&gt;to get a result of 2, as expected.&lt;/SPAN&gt;&lt;EM&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/EM&gt;&lt;BR /&gt;What is going on?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Secondly and some more context, I am accessing a large table via sde connection.&amp;nbsp;&lt;BR /&gt;I wish to then run an update cursor on the (2) selected table items, but using the &lt;EM&gt;&lt;SPAN&gt;itemsToRetire &lt;/SPAN&gt;&lt;/EM&gt;variable in this update cursor does not seem to work.&amp;nbsp; It hangs and eventually says the table cannot be updated.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;SPAN&gt;sTable_path contains the full path to the actual table, going through a sde connection.&amp;nbsp;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;SDE_Workspace_Path contains just the path to the SDE connection, not including the specific table&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;SPAN&gt;&lt;SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;Here is a overview of the code, I have set up an edit session before accessing sTablePath using:&lt;BR /&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;        edit = arcpy.da.Editor(SDE_Workspace_Path) #opening workspace path
        edit.startEditing(False, True) 
        edit.startOperation()
        try:

            itemsToRetire, itemsToRetireCount = arcpy.SelectLayerByAttribute_management(sTable_path, "NEW_SELECTION", sOldWhere)
            #selection_count = int(arcpy.GetCount_management(sTable_path).getOutput(0))
            print(f"!!!Selected {itemsToRetireCount} items for update using where clause: {sOldWhere}!!!")

...

        with arcpy.da.UpdateCursor(itemsToRetire, ("X", "Y")) as oRows:
            &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;but my update cursor won't work, and has a generic error of, cannot update table.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Thanks.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Mar 2024 02:15:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-selectlayerbyattribute-management-issues-how/m-p/1397093#M70073</guid>
      <dc:creator>EdX1</dc:creator>
      <dc:date>2024-03-18T02:15:04Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.SelectLayerByAttribute_management Issues - how to use with an update cursor.</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-selectlayerbyattribute-management-issues-how/m-p/1397221#M70077</link>
      <description>&lt;P&gt;It is best to keep a post to a single question because asking multiple questions in the same post tends to make the comments a mess when some people will reply to one question, some to the other, and some to both.&amp;nbsp; These two questions are closely related enough one could argue it is just one question, but please keep this in mind for future questions.&lt;/P&gt;&lt;P&gt;Regarding your first question of Get Count, it is expected behavior.&amp;nbsp; As its name states, Select Layer By Attribute works on layers, not data sets.&amp;nbsp; Somewhere back a few years ago, Esri decided to augment Select Layer By Attribute to accept an input data set and it will create a layer, select on it, and return it.&amp;nbsp; This is what is happening in your case.&amp;nbsp; When you use Get Count on the data set directly, it is returning the total number of records on the data set, which is should.&lt;/P&gt;</description>
      <pubDate>Mon, 18 Mar 2024 13:33:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-selectlayerbyattribute-management-issues-how/m-p/1397221#M70077</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2024-03-18T13:33:47Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.SelectLayerByAttribute_management Issues - how to use with an update cursor.</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-selectlayerbyattribute-management-issues-how/m-p/1397546#M70082</link>
      <description>&lt;P&gt;Hello, would using&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;SPAN&gt;arcpy.MakeTableView_management(sTable_path, "TableView", where_clause=sOldWhere)&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;be more appropriate in this case given I am accessing a table? If so, how can I then run an update cursor on this temporary table which this creates and save it to my main dataset? Or should I save this to a variable instead of "tableview"? I will be running this code via the terminal of arcPro.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thanks.&lt;/SPAN&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 18 Mar 2024 22:17:28 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-selectlayerbyattribute-management-issues-how/m-p/1397546#M70082</guid>
      <dc:creator>EdX1</dc:creator>
      <dc:date>2024-03-18T22:17:28Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.SelectLayerByAttribute_management Issues - how to use with an update cursor.</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-selectlayerbyattribute-management-issues-how/m-p/1397559#M70083</link>
      <description>&lt;P&gt;also, documentation:&amp;nbsp;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/select-layer-by-attribute.htm," target="_self"&gt;https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/select-layer-by-attribute.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Says it takes a table view as a input dataset. the data I am providing is a table. Is this not correct? as its a table view, rather than a table directly?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 18 Mar 2024 22:33:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-selectlayerbyattribute-management-issues-how/m-p/1397559#M70083</guid>
      <dc:creator>EdX1</dc:creator>
      <dc:date>2024-03-18T22:33:18Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.SelectLayerByAttribute_management Issues - how to use with an update cursor.</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-selectlayerbyattribute-management-issues-how/m-p/1397828#M70085</link>
      <description>&lt;P&gt;If you want to run a cursor against the selected records then pass itemsToRetire to the cursor since it is a table view or layer (depends on data source) that has the records selected.&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2024 14:01:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-selectlayerbyattribute-management-issues-how/m-p/1397828#M70085</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2024-03-19T14:01:30Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.SelectLayerByAttribute_management Issues - how to use with an update cursor.</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-selectlayerbyattribute-management-issues-how/m-p/1398004#M70086</link>
      <description>&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/3.1/arcpy/data-access/updatecursor-class.htm#:~:text=String-,where_clause,-An%20optional%20expression" target="_self"&gt;Update Cursor&lt;/A&gt; accepts a where clause itself so there really is no need to select by attributes first.&amp;nbsp;&lt;/P&gt;&lt;P&gt;just put the expression in the cursor itself.&lt;/P&gt;&lt;P&gt;R_&lt;/P&gt;</description>
      <pubDate>Tue, 19 Mar 2024 17:45:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-selectlayerbyattribute-management-issues-how/m-p/1398004#M70086</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2024-03-19T17:45:18Z</dc:date>
    </item>
    <item>
      <title>Re: arcpy.SelectLayerByAttribute_management Issues - how to use with an update cursor.</title>
      <link>https://community.esri.com/t5/python-questions/arcpy-selectlayerbyattribute-management-issues-how/m-p/1409609#M70308</link>
      <description>&lt;P&gt;The issue was the result of incorrectly using&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;edit.startEditing(False, True)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As this table was not versioned, this caused the script to hang for a long period of time. To get this to work I should have done False, False.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2024 06:10:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/arcpy-selectlayerbyattribute-management-issues-how/m-p/1409609#M70308</guid>
      <dc:creator>EdX1</dc:creator>
      <dc:date>2024-04-15T06:10:22Z</dc:date>
    </item>
  </channel>
</rss>

