anyone has an example to call stored proc from ArcSDESQLExecute (arcpy)? any help will be greatly appreciated
Adding SET NOCOUNT ON works for me to be able to call stored procedures using arcpy.ArcSDESQLExecute. This was last tested with version 10.5.1
This thread is old, but maybe this will help someone. I couldn't get this function to work. It kept returning True instead of rows. From the help doc we know what a Boolean return means. This isn't particularly helpful if you know a stored procedure returns rows. But at least it is something.
....for statements that do not return rows, it will return an indication of the success or failure of the statement (True for success; None for failure).<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
When i turned to pandas as an alternative I received a similar error message from sqlalchemy:
File <SPAN class="string token">"C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\lib\site-packages\sqlalchemy\engine\result.py"</SPAN><SPAN class="punctuation token">,</SPAN> line <SPAN class="number token">1077</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="keyword token">in</SPAN> _non_result <SPAN class="string token">"This result object does not return rows. "</SPAN> sqlalchemy<SPAN class="punctuation token">.</SPAN>exc<SPAN class="punctuation token">.</SPAN>ResourceClosedError<SPAN class="punctuation token">:</SPAN> This result object does <SPAN class="operator token">not</SPAN> <SPAN class="keyword token">return</SPAN> rows<SPAN class="punctuation token">.</SPAN> It has been closed automatically<SPAN class="punctuation token">.</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
This led me to a stackoverflow comment that suggested using SET NO COUNT when working with stored procedures and sqlalchemy. I wondered if this might work with ArcSDESQLExecute. It didn't. But, it solved my problem with pandas. Here is an example if you want the result of a stored procedure and have hit a wall with this function.
<SPAN class="comment token"># Dependencies: ArcGIS PRO 1.4.1 (Python 3.5.4)</SPAN> <SPAN class="comment token"># Currently using python instance: C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3\python.exe</SPAN> <SPAN class="comment token"># SQLAlchemy and pyodbc were added manually to arcgis pro interface</SPAN> <SPAN class="keyword token">import</SPAN> urllib <SPAN class="keyword token">import</SPAN> sqlalchemy <SPAN class="keyword token">as</SPAN> sa <SPAN class="keyword token">import</SPAN> pandas <SPAN class="keyword token">as</SPAN> pd <SPAN class="comment token">#utilizing SET NOCOUNT</SPAN> sqlStatement <SPAN class="operator token">=</SPAN> <SPAN class="string token">"SET NOCOUNT ON EXEC dbo.spPropCharaSelectByNBHD 312524"</SPAN> <SPAN class="comment token">#connection string pointing to DSN created with ODBC Data Source Admin</SPAN> params <SPAN class="operator token">=</SPAN> urllib<SPAN class="punctuation token">.</SPAN>parse<SPAN class="punctuation token">.</SPAN>quote_plus<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"DSN={yourDSNnameHERE};Trusted_Connection=yes"</SPAN><SPAN class="punctuation token">)</SPAN> engine <SPAN class="operator token">=</SPAN> sa<SPAN class="punctuation token">.</SPAN>create_engine<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"mssql+pyodbc:///?odbc_connect=%s"</SPAN> <SPAN class="operator token">%</SPAN> params<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#create pandas dataframe</SPAN> current_source <SPAN class="operator token">=</SPAN> pd<SPAN class="punctuation token">.</SPAN>read_sql_query<SPAN class="punctuation token">(</SPAN>sqlStatement<SPAN class="punctuation token">,</SPAN>engine<SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Did you find a way to execute stored procedures?
That is an example for simple sql statements not stored proc which will not work in my case.
Here is an example from the help docs: ArcSDESQLExecute—Help | ArcGIS Desktop
https://community.esri.com/groups/geodatabase?sr=search&searchId=541e9567-7aec-43e8-9fe8-a92e954e1c4c&searchIndex=0 https://community.esri.com/community/developers/gis-developers/python?sr=search&searchId=cfbdcbe2-5f0d-436e-b26a-4f01ca453aeb&searchIndex=0
Signed in members can post, follow updates, and more. New here? Register a free account.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.