Invalid SQL syntax: SQLSTATE=42601

305
2
02-25-2021 07:54 AM
CliveSwan
Occasional Contributor II

Greetings,

I am trying to use arcpy.da.SearchCursor to return a row, and get the extent (min, max).

I used the ESRI example: Use SQL TOP to limit the number of records to return.

# Use SQL TOP to sort field values
for row in arcpy.da.SearchCursor(fc, fields, sql_clause=('TOP 3', None)):

 

My code: gets an error at 'for row in arcpy.da...'

RuntimeError: Invalid SQL syntax [ERROR: syntax error at or near "3"??

 

Appreciate any pointers,

Regards,

Clive

 

for dirpath, dirnames, filenames in arcpy.da.Walk(workspace, topdown=True, datatype=data_type): 
    #datasets = [''] + datasets if datasets is not None else []
    for filename in filenames:
        #print(filename)  ### Prints the path + name
        fc_name = filename.split('.')[-1]
        print(fc_name)

		sc = arcpy.da.SearchCursor(r"D:\Users\me\db.sde\fc",["polygon"])
		for row in sorted(arcpy.da.SearchCursor(sc, ["Shape@"], sql_clause=('TOP 1', None)):
		print(row)

 

I get an error @ 

0 Kudos
2 Replies
DavinWalker2
Esri Contributor

Hi Clive,

Is your database and SQL Server  or MS Access database? TOP is only supported by SQL Server and MS Access databases.

VinceAngelo
Esri Esteemed Contributor

Good call.  The intertubes says the "SQLSTATE=42601" error is associated with IBM DB2.

- V