<?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: Ceating a SELECT query in arcpy.da.SearchCursor in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/ceating-a-select-query-in-arcpy-da-searchcursor/m-p/1033350#M60234</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/9619"&gt;@CliveSwan&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;You can also use the TOP clause&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

fc = 'c:/data/base.mdb/well'
fields = ['WELL_ID', 'WELL_TYPE']

# Use SQL TOP to sort field values
for row in arcpy.da.SearchCursor(fc, fields, sql_clause=('TOP 3', None)):
    print('{0}, {1}'.format(row[0], row[1]))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ref&amp;nbsp;&lt;A href="https://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-data-access/searchcursor-class.htm" target="_blank"&gt;SearchCursor—Help | ArcGIS for Desktop&lt;/A&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 05 Mar 2021 13:13:43 GMT</pubDate>
    <dc:creator>HenryLindemann</dc:creator>
    <dc:date>2021-03-05T13:13:43Z</dc:date>
    <item>
      <title>Ceating a SELECT query in arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/ceating-a-select-query-in-arcpy-da-searchcursor/m-p/1033328#M60229</link>
      <description>&lt;P&gt;If I SELECT ALL the query, without the select works.&lt;/P&gt;&lt;P&gt;I only want to loop through 5 - 10 records, not 30,000. So trying to get a select statement to limit the number of records returned.&lt;/P&gt;&lt;P&gt;This code works:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    for filename in filenames:
        try:
            for row in (arcpy.da.SearchCursor(filename, ['Shape@'])):               
                print(filename)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The sql_clause throws an error??&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    for filename in filenames:
        try:
            for row in arcpy.da.SearchCursor(filename, ['OBJECTID@', 'Shape@'], sql_clause=('select * from ' + ['OBJECTID@', 'Shape@'] + 'limit 5', None)):
                print(filename)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate any pointers to fixing the sql_clause.&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Clive&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 11:41:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/ceating-a-select-query-in-arcpy-da-searchcursor/m-p/1033328#M60229</guid>
      <dc:creator>CliveSwan</dc:creator>
      <dc:date>2021-03-05T11:41:03Z</dc:date>
    </item>
    <item>
      <title>Re: Ceating a SELECT query in arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/ceating-a-select-query-in-arcpy-da-searchcursor/m-p/1033341#M60231</link>
      <description>&lt;P&gt;have you tried the where clause to limit the object ids queried?&lt;/P&gt;&lt;LI-CODE lang="python"&gt;whr = "OBJECTID &amp;gt;= 3 AND OBJECTID &amp;lt;= 9"   # -- limit to records 3 through 9

c = arcpy.da.SearchCursor(in_fc, ('OID@', 'SHAPE@'), whr, SR)

for i in c:
    print(c)
    
(3, &amp;lt;Polygon object at 0x228a22acd08[0x228a22d75a0]&amp;gt;)
(4, &amp;lt;Polygon object at 0x228a22ac208[0x228a22d7db0]&amp;gt;)
(5, &amp;lt;Polygon object at 0x228a22acd08[0x228a22d75a0]&amp;gt;)
(6, &amp;lt;Polygon object at 0x228a22ac208[0x228a22d7db0]&amp;gt;)
(7, &amp;lt;Polygon object at 0x228a22acd08[0x228a22d75a0]&amp;gt;)
(8, &amp;lt;Polygon object at 0x228a22ac208[0x228a22d7db0]&amp;gt;)&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 05 Mar 2021 12:30:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/ceating-a-select-query-in-arcpy-da-searchcursor/m-p/1033341#M60231</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-03-05T12:30:38Z</dc:date>
    </item>
    <item>
      <title>Re: Ceating a SELECT query in arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/ceating-a-select-query-in-arcpy-da-searchcursor/m-p/1033349#M60233</link>
      <description>&lt;P&gt;The documentation for the 'sql_clause' parameter says that it takes an optional pair of prefix and postfix clauses organized in a list or tuple.&amp;nbsp;&lt;/P&gt;&lt;P&gt;"An SQL prefix clause is positioned in the first position and will be inserted between the SELECT keyword and the SELECT COLUMN LIST. The SQL prefix clause is most commonly used for clauses such as DISTINCT or ALL."&lt;/P&gt;&lt;P&gt;"An SQL postfix clause is positioned in the second position and will be appended to the SELECT statement, following the where clause. The SQL postfix clause is most commonly used for clauses such as ORDER BY."&lt;/P&gt;&lt;P&gt;Giving it a full sql query will make it fail.&amp;nbsp; Dan's post would be how to get a number of records.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 13:02:51 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/ceating-a-select-query-in-arcpy-da-searchcursor/m-p/1033349#M60233</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2021-03-05T13:02:51Z</dc:date>
    </item>
    <item>
      <title>Re: Ceating a SELECT query in arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/ceating-a-select-query-in-arcpy-da-searchcursor/m-p/1033350#M60234</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/9619"&gt;@CliveSwan&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;You can also use the TOP clause&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

fc = 'c:/data/base.mdb/well'
fields = ['WELL_ID', 'WELL_TYPE']

# Use SQL TOP to sort field values
for row in arcpy.da.SearchCursor(fc, fields, sql_clause=('TOP 3', None)):
    print('{0}, {1}'.format(row[0], row[1]))&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ref&amp;nbsp;&lt;A href="https://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-data-access/searchcursor-class.htm" target="_blank"&gt;SearchCursor—Help | ArcGIS for Desktop&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 13:13:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/ceating-a-select-query-in-arcpy-da-searchcursor/m-p/1033350#M60234</guid>
      <dc:creator>HenryLindemann</dc:creator>
      <dc:date>2021-03-05T13:13:43Z</dc:date>
    </item>
    <item>
      <title>Re: Ceating a SELECT query in arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/ceating-a-select-query-in-arcpy-da-searchcursor/m-p/1033389#M60236</link>
      <description>&lt;P&gt;Hi Henry,&lt;/P&gt;&lt;P&gt;I wish ESRI would provide context about the code.&lt;/P&gt;&lt;P&gt;That SearchCursor code example was my first stab. Unfortunately the TOP option is only for MS Access and SQL Server.&lt;/P&gt;&lt;P&gt;Thanks Dan, your pointer with the Where statement helped me solve the issue.&lt;/P&gt;&lt;P&gt;Much appreciated.&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 15:22:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/ceating-a-select-query-in-arcpy-da-searchcursor/m-p/1033389#M60236</guid>
      <dc:creator>CliveSwan</dc:creator>
      <dc:date>2021-03-05T15:22:01Z</dc:date>
    </item>
    <item>
      <title>Re: Ceating a SELECT query in arcpy.da.SearchCursor</title>
      <link>https://community.esri.com/t5/python-questions/ceating-a-select-query-in-arcpy-da-searchcursor/m-p/1033413#M60237</link>
      <description>&lt;P&gt;No problem.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 05 Mar 2021 16:10:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/ceating-a-select-query-in-arcpy-da-searchcursor/m-p/1033413#M60237</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2021-03-05T16:10:11Z</dc:date>
    </item>
  </channel>
</rss>

