1

Trying to copy features from sde to fgdb but only when the objectid is below a certain number.

outpath = r'C:\Users\CHOK\Desktop\SDE Conversion Project\TEST.gdb'
query = '"objectid" < ' + "'121'"
for featureclass in dataset: 
        if featureclass == 'Contour_10m':
            output = outpath + os.sep + featureclass
            arcpy.Select_analysis(featureclass, output, query)

The code executes but only the schema of the sde is copied in the new feature class created in the gdb and no data with objectid<121 that I expected would be copied over is present.

Any ideas on what the issue is?

closed as off-topic by Vince, BERA, Kadir Şahbaz, Erik, Jochen Schwarze Mar 28 at 13:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This problem cannot or can no longer be reproduced. Changes to the system or to the asker's circumstances may have rendered the question obsolete, or the question does not include a procedure to enable potential answerers to reproduce the same symptoms. Such questions are off-topic as they are unlikely to help future readers, but editing them to include more details can lead to re-opening." – Vince, BERA, Kadir Şahbaz, Erik, Jochen Schwarze
If this question can be reworded to fit the rules in the help center, please edit the question.

  • You're testing objectid (Long Integer) with a string, query = 'objectid < 121' will do the job. Better still query = '{} < 121'.format(arcpy.Describe(featureclass).OIDFieldName) to get the name of the OID field, Objectid for geodatabases, FID for shapefiles - makes the code more flexible. Also output = os.path.join(outpath,featureclass) instead of simple concatenation is good practice, it automatically inserts os.sep between the strings. – Michael Stimson Mar 28 at 2:34
0

In your selection query you are expressing 121 as a string '121', as your objectid field is a long integer type the less than operator < doesn't work with a mismatched data type. To fix up your code in the easiest way:

query = '"objectid" < 121'
for featureclass in dataset: 
        if featureclass == 'Contour_10m':
            output = os.path.join(outpath,featureclass) 
            arcpy.Select_analysis(featureclass, output, query)

However, to make the code more flexible:

for featureclass in dataset:
    if featureclass == 'Contour_10m':
        query = '{} < 121'.format(arcpy.Describe(featureclass).OIDFieldName)
        output = os.path.join(outpath,featureclass) 
        arcpy.Select_analysis(featureclass, output, query)

Using the Describe statement to find the table property OIDFieldName will ensure that the code works for all data types that have an OID (most but not all do).

Describe is extremely useful to access properties about all different types of data; on the help page there are a number of different data type properties, any given dataset may, and usually do, have properties from multiple types.. in this instance arcpy.Describe(featureclass) has dataset, feature class and table properties in addition to the default any kind of data properties.

Not the answer you're looking for? Browse other questions tagged or ask your own question.