AnsweredAssumed Answered

Using python to query a field in a layer

Question asked by NEOMAPPER on May 18, 2018

I am trying to query a field in a layer but it fails converting the nvarchar value '65N' to data type int. Is there a way I can prevent it from converting and simply just run a query and print the results?

Runtime error 
Traceback (most recent call last):
  File "<string>", line 7, in <module>
RuntimeError: Underlying DBMS error [[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Conversion failed when converting the nvarchar value '65N' to data type int.] [NEO_1.SDE.P_Meters]

 

import arcpy

fc = 'Database Connections\SDE.sde\NEO_1.SDE.P_PipeSystem\NEO_1.SDE.P_Meters'
field = 'SYSTEMNUMBER'

cursor = arcpy.da.SearchCursor(fc, (field, "SYSTEMNUMBER"), """"SYSTEMNUMBER" = 009""")
for row in cursor:
    # Print the name of the different gas systems
    #
    print(row[1])
    result = arcpy.GetCount_management(fc)
    print('{} has {} records'.format(row, result[0]))

Outcomes