Select to view content in your preferred language

Execute Sql Statement in SQL Server 2008 with ArcSDE connection

3384
11
Jump to solution
07-29-2013 01:52 AM
ben_abdallahmhd
Deactivated User
hi,
i'm trying to execute this SqlStatement to establish an sql query in sql server 2008 with an arcsde connection (arcsde personel use)

but there is no result.

the sql statement is :

Execute SQL Statement: (SELECT * FROM [TOP10_SIG].[dbo].[ABCBNIVL] WHERE [TOP10_SIG].[dbo].[ABCBNIVL].[TYPECBN] = 3)

an idea please !
I use ArcGIS 10.0
Arcsde personal use

Code:

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.
Tags (2)
0 Kudos
11 Replies
ben_abdallahmhd
Deactivated User
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_



hi rzufelt,

you have reason the type of the field is SubtypeField, when i change the query to another field (integer) it works very good.

thank you very much

for the subtype field, have you please an idea how can select  it.


>>> 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
.
.
0 Kudos
RhettZufelt
MVP Notable Contributor
Ben,

I don't use sde or subtypes, so can't really test it.  I'd look here: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/A_quick_tour_of_using_SQL_with_ArcSDE_... and the sub folders under that link.  Might get you what you need.

R_
0 Kudos