2

In ArcGIS Desktop, I can use various tools to interactively select features. These selection tools are handy for making complex selections (tabular or spatial) that would be otherwise cumbersome to perform.

I would like to create an SQL WHERE clause from my selected features so that I can use the query/selection in programs other than ArcGIS Desktop.

For example, using the selected features in ArcMap, create a string of OBJECTIDs:

where objectid in 
        ('5982722', '2916887', '1932266', '7961145', '9767401', 
        '5028271', '1280191', '1766943', '1539731', '7509119', 
        '283535', '2908486', '4728132', '1292375', '1376223', 
        '382195', '6840113', '1609989', '3346030', '3617504', 
        '5087102', '6160716', '177817', '2271321', '3535963', 
        '9511098', '739973', '1960256', '660265', '9496836', 
        '6976042', '6830024', '6618473', '8688161', '9325979', 
        '9736524', '5834279', '1704444', '8568749', '7174666', 
        '4697259', '4614739', '8327862', '1177620', '539499')

How can I do this?

I would like the solution to be a quick & easy process (3 clicks or less), to be used many times daily.

2

I can do this using ArcPy's describe.fidset function:

  1. Get a list of objectids using describe.fidset. The list is delimited with a ;
  2. Replace the ; with ', '
  3. Concatenate where objectid in ( ' + the list of objectids + ' )

import arcpy
tablename = "USER.A_TEST_FC"
layerdesc = (arcpy.Describe(tablename))
whereclause = str(layerdesc.FIDSet)
whereclause = whereclause.replace(";",  "', '" )
print "select * from " + tablename
print "where objectid in ( '" + whereclause + "' )"

Output:

select * from USER.A_TEST_FC 
where objectid in ('5982722', '2916887', '1932266', '7961145', '9767401', '5028271', '1280191', '1766943', '1539731', '7509119', '283535', '2908486', '4728132', '1292375', '1376223', '382195', '6840113', '1609989', '3346030', '3617504', '5087102', '6160716', '177817', '2271321', '3535963', '9511098', '739973', '1960256', '660265', '9496836', '6976042', '6830024', '6618473', '8688161', '9325979', '9736524', '5834279', '1704444', '8568749', '7174666', 
 '4697259', '4614739', '8327862', '1177620', '539499')

Unfortunately, it does take more than "3 clicks" to excecute. I suppose a python add-in might be quicker to run than pasting & modifying a script.

  • 1
    When you pass layer as parameter to script, arcpy honors selection, ie it doesn't see the rest of the records. – FelixIP Sep 22 '17 at 21:05
0

From jbalk in a related question:

OIDlist = []
with arcpy.da.SearchCursor(layer, 'OBJECTID') as scur:
     for row in scur:
         OIDlist.append(row[0])

Once I have this list, I can assemble (concatenate) a where clause, just as I did in my other answer.

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.