import arcpy from arcpy import env import sys import os arcpy.env.workspace = (r"c:\connectionFiles\conntop10sig.sde") try: SQLStatement = "SELECT * FROM [TOP10_SIG].[dbo].[ABCBNIVL] WHERE [TOP10_SIG].[dbo].[ABCBNIVL].[TYPECBN] = 3 " SQLStatementList = SQLStatement.split(";") print "\n" for sql in SQLStatementList: print "Execute SQL Statement: " + sql try: sdeReturn = sdeConn.execute(sql) except Exception, ErrorDesc: print "ErrorDesc" sdeReturn = False if isinstance(sdeReturn, list): print "Number of rows returned by query: " + len(sdeReturn), "rows" for row in sdeReturn: print "row" print "\n" else: if sdeReturn == True: print "SQL statement: " + sql + " ran sucessfully." print "\n" else: print "SQL statement: " + sql + " FAILED." print "\n" except Exception, ErrorDesc: print "Exception, ErrorDesc" except: print "Problem executing SQL.
Solved! Go to Solution.
Try this:import arcpy from arcpy import * import sys arcpy.env.workspace = r"c:\connectionFiles\conntop10sig.sde" sql = "select * from TOP10_SIG.DBO.ABCBNIVL where TOP10_SIG.DBO.ABCBNIVL.TYPECBN = 3" sdeConn = arcpy.ArcSDESQLExecute(r"c:\connectionFiles\conntop10sig.sde") print "Execute SQL Statement: ", sql print "connecting to dba" sdeReturn = sdeConn.execute(sql) if isinstance(sdeReturn, list): print "Number of rows returned by query: ", len(sdeReturn), "rows" for row in sdeReturn: print row else: print "no selection made"
If you are still getting errors, you need to confirm that there is a numeric field named TYPECBN in the table named TOP10_SIG.DBO.ABCBNIVL in the sde connection r"c:\connectionFiles\conntop10sig.sde". Look in ArcCatalog and make sure that TOP10_SIG.DBO.ABCBNIVL is listed as the table name there. If so, and still getting errors, you might try dropping the DBO from it ( TOP10_SIG.ABCBNIVL ) as I have seen in the FlexApp environment where links to DBO tables didn't work with that in the path.
R_
>>> import arcpy
... from arcpy import *
... import sys
... arcpy.env.workspace = (r"c:\connectionFiles\conntop10sig.sde")
... try:
... SQLStatement = ("select * from TOP10_SIG.DBO.ABCBNIVL where TOP10_SIG.DBO.ABCBNIVL.ALTDCRNV = 50") # ALTDCRNV = the elevation
... sdeConn = arcpy.ArcSDESQLExecute(r"c:\connectionFiles\conntop10sig.sde") # vital part you are missing....
... print "Execute SQL Statement: ", SQLStatement
... try:
... # Pass the SQL statement to the database.
... #
... print "connecting to dba"
... sdeReturn = sdeConn.execute(SQLStatement)
... print "Number of rows returned by query: ", len(sdeReturn)
... except Exception, ErrorDesc:
... print "ErrorDesc",ErrorDesc ## added this to actually print what the error is
... sdeReturn = False
... # If the return value is a list (a list of lists), display each list as a row from the
... # table being queried.
... if isinstance(sdeReturn, list):
... print "Number of rows returned by query: ", len(sdeReturn), "rows"
... for row in sdeReturn:
... print "row"
... print "\n"
... else:
... # If the return value was not a list, the statement was most likely a DDL statment.
... # Check its status.
... if sdeReturn == True:
... print "SQL statement: ", SQLStatement , " ran sucessfully."
... print "\n"
... else:
... print "SQL statement: ",SQLStatement ," FAILED."
... print "\n"
... except:
... print "Problem executing SQL."
...
...
...
Execute SQL Statement: select * from TOP10_SIG.DBO.ABCBNIVL where TOP10_SIG.DBO.ABCBNIVL.ALTDCRNV = 50
connecting to dba
Number of rows returned by query: 38
Number of rows returned by query: 38 rows
row
row
.
.