<?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: Using SearchCursor with UpdateCursor in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/using-searchcursor-with-updatecursor/m-p/180937#M13898</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all, I'm trying to re-write this to run through SQL Server for performance reasons.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is what I have, but it is not generating the sequence from 1 to n, it is starting at the row number of the PARTITION_BY and GROUP_BY statement. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;UPDATE CPTESTPOINTINSPECTION
SET CPTESTPOINTINSPECTION.SEQ_NO = seq

FROM 
(
SELECT t.TECH_ID,i.GAS_CORR_STRUCT_NO, INSPECTIONDATE, SEQ_NO, ROW_NUMBER() OVER (PARTITION BY t.TECH_ID ORDER BY i.GAS_CORR_STRUCT_NO) AS Seq
FROM ASSIGNED_TECHS t
INNER JOIN GISDG..CONVERSION.CPTESTPOINTINSPECTION i
ON i.ASSIGNED_USER&amp;nbsp; = t.TECH_ID
) CPTESTPOINTINSPECTION
WHERE CPTESTPOINTINSPECTION.INSPECTIONDATE IS NULL&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I know this isn't a SQL forum but I figured I'd ask anyway, I know there are very talented people here, so I figured I'd give it a shot.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 09:14:53 GMT</pubDate>
    <dc:creator>MarcCusumano</dc:creator>
    <dc:date>2021-12-11T09:14:53Z</dc:date>
    <item>
      <title>Using SearchCursor with UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/using-searchcursor-with-updatecursor/m-p/180932#M13893</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello, I'm trying to use an UpdateCursor inside of a SearchCursor to populate sequence numbers on an inspection table according to a parcitular UserID. Basically the SearchCursor iterates through a small table with a list of about 29 user ID's (the table changes over time) and uses the values in the where_clause of the updatecursor. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The UpdateCursor sorts the order of the input table based on a structure number and populates the sequences from 1 to 0. Here is the code I have tried:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;techrows = arcpy.SearchCursor(psegGasGDB_InputPrefix + "ASSIGNED_TECHS")&amp;nbsp; for row in techrows:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; if TechID != row.ASSIGNED_USER:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; TechID = row.ASSIGNED_USER &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where_clause = "ASSIGNED_USER = " + TechID &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows = arcpy.UpdateCursor(CONVERSION_InputPrefix + "CPTestPointInspection", where_clause, "SEQ_NO; GAS_CORR_STRUCT_NO", "GAS_CORR_STRUCT_NO")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in rows: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print row &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stop = 1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.SetValue("SEQ_NO", stop) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stop = stop + 1&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print e &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; logging.error(": %s" %(e))&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; del rows &amp;nbsp;&amp;nbsp;&amp;nbsp; del techrows &lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But this gives an "Error 999999: Index passed was not within the valid range". Can't figure out what I'm doing wrong...&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Aug 2012 18:30:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-searchcursor-with-updatecursor/m-p/180932#M13893</guid>
      <dc:creator>MarcCusumano</dc:creator>
      <dc:date>2012-08-30T18:30:34Z</dc:date>
    </item>
    <item>
      <title>Re: Using SearchCursor with UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/using-searchcursor-with-updatecursor/m-p/180933#M13894</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;First off, using nested cursors is generally a bad idea. Extracting the information from your search cursor and storing them in a dictionary to access with your update cursor is the preferred method. That being said, your issue is you define the row object for both cursors as the same variable, causing the code to implode. Also it looks like your indentation may be off.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Aug 2012 18:57:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-searchcursor-with-updatecursor/m-p/180933#M13894</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-08-30T18:57:31Z</dc:date>
    </item>
    <item>
      <title>Re: Using SearchCursor with UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/using-searchcursor-with-updatecursor/m-p/180934#M13895</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;What kind of value is &lt;/SPAN&gt;&lt;STRONG&gt;ASSIGNED_USER&lt;/STRONG&gt;&lt;SPAN&gt;? If it is a string value then I think the where clause you specify here is going to fail, in fact it will fail anyway because it is not delimitted correctly:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;where_clause = "ASSIGNED_USER = " + TechID&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;that probably needs to look more like one of these:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;where_clause = """ "ASSIGNED_USER" = """ + TechID # if TechID is numeric
where_clause = """ "ASSIGNED_USER" = """ + "'"+TechID+"'" # if TechID is string&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Beyond this, I think the logic of running a cursor inside another cursor is challenging to pull off, and am wondering if there may be some confusion between the two different row objects that are being referenced as 'row' - you don't need to name those row objects 'row', you can just give them any name, so maybe changing one of those to be a different name will make a difference. What you might do instead is use your search cursor to create a list of valid Users, then iterate through that list in a for loop, supplying each user ID to the where clause in your update cursor.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 16:09:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-searchcursor-with-updatecursor/m-p/180934#M13895</guid>
      <dc:creator>ChristopherThompson</dc:creator>
      <dc:date>2021-12-12T16:09:13Z</dc:date>
    </item>
    <item>
      <title>Re: Using SearchCursor with UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/using-searchcursor-with-updatecursor/m-p/180935#M13896</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Looking at it again, probably the biggest problem here is that you are deleting your cursor in the middle of your loop.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This line should be outside your loop.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;del techrows&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 30 Aug 2012 19:25:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-searchcursor-with-updatecursor/m-p/180935#M13896</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2012-08-30T19:25:51Z</dc:date>
    </item>
    <item>
      <title>Re: Using SearchCursor with UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/using-searchcursor-with-updatecursor/m-p/180936#M13897</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thank you Matt, using a dictionary worked well:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;lookupDict = {}
searchRows = arcpy.SearchCursor(psegGasGDB_InputPrefix + "ASSIGNED_TECHS")
searchRow = searchRows.next()

while searchRow:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lookupDict[searchRow.TECH_ID] = [searchRow.TECH_ID]
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; where_clause =&amp;nbsp; """ "ASSIGNED_USER" = """ + "'"+searchRow.TECH_ID+"'"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; updatecursor = arcpy.UpdateCursor(CONVERSION_InputPrefix + "CPTestPointInspection", where_clause, "", "SEQ_NO; GAS_CORR_STRUCT_NO", "GAS_CORR_STRUCT_NO")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stop = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fldSEQNO = "SEQ_NO"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in updatecursor:

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print stop
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stop = stop + 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; row.setValue(fldSEQNO, stop)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cursor.updateRow(row)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print e
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; logging.error(": %s" %(e))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; searchRow = searchRows.next()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
del searchRow
del searchRows&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:14:50 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-searchcursor-with-updatecursor/m-p/180936#M13897</guid>
      <dc:creator>MarcCusumano</dc:creator>
      <dc:date>2021-12-11T09:14:50Z</dc:date>
    </item>
    <item>
      <title>Re: Using SearchCursor with UpdateCursor</title>
      <link>https://community.esri.com/t5/python-questions/using-searchcursor-with-updatecursor/m-p/180937#M13898</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi all, I'm trying to re-write this to run through SQL Server for performance reasons.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is what I have, but it is not generating the sequence from 1 to n, it is starting at the row number of the PARTITION_BY and GROUP_BY statement. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;UPDATE CPTESTPOINTINSPECTION
SET CPTESTPOINTINSPECTION.SEQ_NO = seq

FROM 
(
SELECT t.TECH_ID,i.GAS_CORR_STRUCT_NO, INSPECTIONDATE, SEQ_NO, ROW_NUMBER() OVER (PARTITION BY t.TECH_ID ORDER BY i.GAS_CORR_STRUCT_NO) AS Seq
FROM ASSIGNED_TECHS t
INNER JOIN GISDG..CONVERSION.CPTESTPOINTINSPECTION i
ON i.ASSIGNED_USER&amp;nbsp; = t.TECH_ID
) CPTESTPOINTINSPECTION
WHERE CPTESTPOINTINSPECTION.INSPECTIONDATE IS NULL&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I know this isn't a SQL forum but I figured I'd ask anyway, I know there are very talented people here, so I figured I'd give it a shot.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 09:14:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/using-searchcursor-with-updatecursor/m-p/180937#M13898</guid>
      <dc:creator>MarcCusumano</dc:creator>
      <dc:date>2021-12-11T09:14:53Z</dc:date>
    </item>
  </channel>
</rss>

