<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How do I transfer a SQL variable back into an Arcpy script? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-do-i-transfer-a-sql-variable-back-into-an/m-p/676007#M52350</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If they are SDE tables then why not just use arcpy.ListTables() to access and work with cursors?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you truly need to access outside of the ESRI stack, I'd start with just issuing the desired T-SQL against the database using pyodbc.&amp;nbsp; This doesn't give you an solution for Stored Procedures but might start you off down that path.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Adapted from existing implementation but untested:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Update (referencing Sproc with parameters seems pretty straight forward):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;sqlcursor.execute("{call dbo.SProName(?,?)}", (param1), (param2))&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sample pyodbc:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="font-family: Consolas;"&gt;import pyodbc&lt;/SPAN&gt;

conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};' +
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'SERVER=' + servername +
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ';DATABASE=' + databasename +
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ';schema=' + schemaname +
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ';UID=' + username +
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ';PWD=' + password)

#I'm not totally sold on this being the best choice of sql
sql = """SELECT 1 WHERE EXISTS (SELECT L_HAZARDPROBABILITY.F_EVENT 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM L_HAZARDPROBABILITY WHERE L_HAZARDPROBABILITY.F_EVENT&amp;lt;10 Or L_HAZARDPROBABILITY.F_EVENT &amp;gt;1000)"""

sqlcursor = conn.cnxn.cursor()
sqlcursor.execute(sql)
sqlrows = sqlcursor.fetchall()

datArray = []
for sqlrow in sqlrows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; datArray.append(sqlrow)

if len(datArray) &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #we have at least 1 row in the array filled by cursor
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'do stuff

sqlcursor.close()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 04:30:17 GMT</pubDate>
    <dc:creator>JamesCrandall</dc:creator>
    <dc:date>2021-12-12T04:30:17Z</dc:date>
    <item>
      <title>How do I transfer a SQL variable back into an Arcpy script?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-transfer-a-sql-variable-back-into-an/m-p/676006#M52349</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I am trying to teach myself to access sql tables in an SDE database from an Arcpy script as part of a larger QC process. I am fairly certain I have the sql statement correct (it works in SQL Server, albeit formatted a little differently -&amp;nbsp; the python window didn't like the line breaks), but I have no clue how to return my declared sql variable back as a python variable so that I may run other stuff on it. Ultimately, I would like to be able to run stored procedures via the script, but baby steps first.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am trying to check a sql table in a SDE Database to see if the value of a single column is smaller than 1000 but larger than 10. If there are values outside of this range, I need to run other processes.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is what I've got so far:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;SDE = r'Database Connections\NC_RISK - test - 106.sde'
&amp;nbsp;&amp;nbsp;&amp;nbsp; SQLLink = arcpy.ArcSDESQLExecute(SDE)
&amp;nbsp;&amp;nbsp;&amp;nbsp; L_HAZARDPROBABILITY = r'\NC_RISK.sql.L_HAZARDPROBABILITY'
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; sql = """LARE @MyBadIndicator int; SET @MyBadIndicator = 0; SET @MyBadIndicator = SELECT 1 WHERE EXISTS (SELECT L_HAZARDPROBABILITY.F_EVENT
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM L_HAZARDPROBABILITY WHERE L_HAZARDPROBABILITY.F_EVENT&amp;lt;10 Or L_HAZARDPROBABILITY.F_EVENT &amp;gt;1000)"""&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;SQL has always been a little intimidating to me (primarily b/c I've just never known where to start). So, I just going to jump in head first. Any help would be greatly appreciated. If there are any recommended resources I should check out to learn this stuff, that would be appreciated as well.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have been playing around with this a little more trying to get something to happen, anything really, and I keep getting the same errors.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the above script, I never execute the sql query. When I do, I receive the following error:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;AttributeError: ArcSDESQLExecute: StreamPrepareSQL ArcSDE Error -37 \ue61c&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I tried changing the query to &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;sql = "SELECT F_EVENT FROM L_HAZARDPROBABILITY WHERE F_EVENT&amp;lt;10"&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and I get the same error.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When I change the code to &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; sql = "SELECT F_EVENT FROM L_HAZARDPROBABILITY WHERE F_EVENT&amp;lt;10"
&amp;nbsp;&amp;nbsp;&amp;nbsp; result = SQLLink.execute(sql)
except Exception as err:
&amp;nbsp;&amp;nbsp;&amp;nbsp; print (err)
&amp;nbsp;&amp;nbsp;&amp;nbsp; result = False

I get 
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #e23d39;"&gt;UnicodeEncodeError: 'ascii' codec can't encode character u'\ue61c' in position 52: ordinal not in range(128)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Message was edited by: John Lay to include further experimentation.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:30:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-transfer-a-sql-variable-back-into-an/m-p/676006#M52349</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2021-12-12T04:30:14Z</dc:date>
    </item>
    <item>
      <title>Re: How do I transfer a SQL variable back into an Arcpy script?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-transfer-a-sql-variable-back-into-an/m-p/676007#M52350</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If they are SDE tables then why not just use arcpy.ListTables() to access and work with cursors?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you truly need to access outside of the ESRI stack, I'd start with just issuing the desired T-SQL against the database using pyodbc.&amp;nbsp; This doesn't give you an solution for Stored Procedures but might start you off down that path.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Adapted from existing implementation but untested:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Update (referencing Sproc with parameters seems pretty straight forward):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;sqlcursor.execute("{call dbo.SProName(?,?)}", (param1), (param2))&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sample pyodbc:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN style="font-family: Consolas;"&gt;import pyodbc&lt;/SPAN&gt;

conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0};' +
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'SERVER=' + servername +
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ';DATABASE=' + databasename +
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ';schema=' + schemaname +
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ';UID=' + username +
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ';PWD=' + password)

#I'm not totally sold on this being the best choice of sql
sql = """SELECT 1 WHERE EXISTS (SELECT L_HAZARDPROBABILITY.F_EVENT 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM L_HAZARDPROBABILITY WHERE L_HAZARDPROBABILITY.F_EVENT&amp;lt;10 Or L_HAZARDPROBABILITY.F_EVENT &amp;gt;1000)"""

sqlcursor = conn.cnxn.cursor()
sqlcursor.execute(sql)
sqlrows = sqlcursor.fetchall()

datArray = []
for sqlrow in sqlrows:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; datArray.append(sqlrow)

if len(datArray) &amp;gt; 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #we have at least 1 row in the array filled by cursor
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 'do stuff

sqlcursor.close()&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:30:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-transfer-a-sql-variable-back-into-an/m-p/676007#M52350</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-12T04:30:17Z</dc:date>
    </item>
    <item>
      <title>Re: How do I transfer a SQL variable back into an Arcpy script?</title>
      <link>https://community.esri.com/t5/python-questions/how-do-i-transfer-a-sql-variable-back-into-an/m-p/676008#M52351</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks James,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Of course I was trying to make things way more difficult than they needed to be. I had originally gone down this route because I was unable to access the sql.L_HAZARDPROBABILITY table after performing a os.path.join on the database and the table name. I left out the very important "sql."&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I will look into this other method when I start down the SQL path again--shouldn't be too long.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 12 Jan 2016 15:22:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-do-i-transfer-a-sql-variable-back-into-an/m-p/676008#M52351</guid>
      <dc:creator>JohnLay</dc:creator>
      <dc:date>2016-01-12T15:22:11Z</dc:date>
    </item>
  </channel>
</rss>

