johnmdye

Exporting Subselection based on Selection in different table

Discussion created by johnmdye on Jun 20, 2013
I have a Table and a Feature Class. I'm trying to create a Selection on the FC based on the Selection in the Table, and export the resulting Selected Features in the FC to a new FC, but I keep getting an invalid expression error.

#Create the Variables
BlockGroups = r"C:\ArcGIS\Business Analyst\US\Demographic Data\BA_2011.gdb\esri_bg"
BG2Branch_ODCM_MDT = r"C:\ArcGIS\PTA_Test.gdb\CBSA_12420_ProForma_ODCM_MDT"

#Make a Feature Layer from the Block Groups
arcpy.MakeFeatureLayer_management(BlockGroups, "BlockGroups_lyr")

# Create unique set of StoreIDs
StoreSet = set([r[0] for r in arcpy.da.SearchCursor(BG2Branch_ODCM_MDT, ["DID"])])

# Create a for loop to iterate through the unique set of StoreIDs
for Store in StoreSet:
    # Make a TableView of the Table using a Where Query to make a Table View only of the records where the 'DestID' is equal to the
    # current Store
    TableView = arcpy.MakeTableView_management(BG2Branch_ODCM_MDT, "BG2Branch_ODCM_MDT_tView", '"DestID" = ' + "'" + str(Store) + "'")
    TableView = TableView.getOutput(0)
    # Select from the BlockGroups Feature Layer, the records which have an 'ID' that matches the values held in the 'OriginID' field
    # in the current selection of the 'BG2Branch_ODCM_MDT' table.
    arcpy.SelectLayerByAttribute_management("BlockGroups_lyr", "NEW_SELECTION", '"ID" IN (SELECT "OriginID" FROM ' + str(TableView) + ")")
    # Export the selected Block Groups to a new Feature Class.
    arcpy.FeatureClassToFeatureClass_conversion("BlockGroups_lyr", r"C:\ArcGIS\SYSTEM\COF\Data\CapitalOne.gdb", "TradeArea_" + str(Store))


I keep receiving the error
Runtime error  Traceback (most recent call last):   File "<string>", line 19, in <module>   File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 6435, in SelectLayerByAttribute     raise e ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).

Outcomes