Hi all,
I am having trouble updating one of my scripts to work with SQL Server 2016. The problem lies when I try to call the sde.next_rowid procedure using the arcpy.ArcSDESQLExecute method. I am using the code that is described in How To: Insert geometry from XY coordinates using SQL and it works just fine in SSMS. However, when it try in it my python, I get the following message. I believe the problem likes with the SQL but it could be with https://community.esri.com/community/developers/gis-developers/python?sr=search&searchId=94336a51-e622-4a11-98a0-c617c9df218e&searchIndex=0. Anyone see anything I am missing?
Thanks,
Kevin
Error Message:
ArcSDESQLExecute: StreamPrepareSQL ArcSDE Extended error 11514 [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]The metadata could not be determined because statement 'EXECUTE sp_executesql @sql, N'@newid INTEGER OUTPUT', @newid = @rowid OUTPUT' in procedure 'next_rowid' contains dynamic SQL. Consider using the WITH RESULT SETS clause to explicitly describe the result set.
Python
<SPAN class="comment token">#Snippet of code with issue.</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy
strSDE <SPAN class="operator token">=</SPAN> <SPAN class="string token">'C:\WorkDoc\Dev\ArchiveRestore\ArchiveRestore\G02TLSNR_VECTOR.sde'</SPAN>
conn <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ArcSDESQLExecute<SPAN class="punctuation token">(</SPAN>strSDE<SPAN class="punctuation token">)</SPAN>
SQL <SPAN class="operator token">=</SPAN> <SPAN class="string token">'''DECLARE @RowCount int\n'''</SPAN>
SQL <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'''SET @RowCount = (SELECT COUNT(*) FROM GISTEST.VECTOR.ELEMBND_old WHERE GDB_TO_DATE > CONVERT(datetime,'2017-06-22 11:37:33', 20))\n'''</SPAN>
SQL <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'''DECLARE @iterator INT\n'''</SPAN>
SQL <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'''SELECT @iterator = MIN(OBJECTID_1) FROM GISTEST.VECTOR.ELEMBND_OLD WHERE GDB_TO_DATE > CONVERT(datetime,'2017-06-22 11:37:33', 20)\n'''</SPAN>
SQL <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'''WHILE @iterator is NOT NULL\n'''</SPAN>
SQL <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'''BEGIN\n'''</SPAN>
SQL <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'''DECLARE @newid int\n'''</SPAN>
SQL <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'''DECLARE @gid uniqueidentifier\n'''</SPAN>
SQL <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'''EXEC GISTEST.sde.next_rowid 'VECTOR', 'ELEMBND_H', @newid OUTPUT\n'''</SPAN>
SQL <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'''EXEC GISTEST.sde.next_globalid @gid OUTPUT\n'''</SPAN>
SQL <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'''INSERT INTO GISTEST.VECTOR.ELEMBND_H (GDB_ARCHIVE_OID, GDB_TO_DATE, GLOBALID, OBJECTID, NAME, NAME_CODE, SCHLNUM, CREATED_USER, CREATED_DATE, LAST_EDITED_USER, LAST_EDITED_DATE, GDB_FROM_DATE, SHAPE)\n'''</SPAN>
SQL <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'''SELECT @newid, CONVERT(datetime, '2017-06-22 11:37:33',20), @gid, OBJECTID, NAME, NAME_CODE, SCHLNUM, CREATED_USER, CREATED_DATE, LAST_EDITED_USER, LAST_EDITED_DATE, GDB_FROM_DATE, SHAPE\n'''</SPAN>
SQL <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'''FROM GISTEST.VECTOR.ELEMBND_old\n'''</SPAN>
SQL <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'''WHERE @iterator = OBJECTID_1;\n'''</SPAN>
SQL <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'''SELECT @iterator = MIN(OBJECTID_1) FROM GISTEST.VECTOR.ELEMBND_OLD WHERE GDB_TO_DATE > CONVERT(datetime,'2017-06-22 11:37:33', 20) AND @iterator < OBJECTID_1;\n'''</SPAN>
SQL <SPAN class="operator token">+=</SPAN> <SPAN class="string token">'''END\n'''</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
sqlResult <SPAN class="operator token">=</SPAN> conn<SPAN class="punctuation token">.</SPAN>execute<SPAN class="punctuation token">(</SPAN>SQL<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN> Exception <SPAN class="keyword token">as</SPAN> err<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> err
sqlResult <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">False</SPAN>
<SPAN class="keyword token">if</SPAN> sqlResult<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"SQL insert command 2 successful"</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="string token">"SQL insert command 2 failed"</SPAN>
<SPAN class="comment token">#Clean up</SPAN>
<SPAN class="keyword token">del</SPAN> conn<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></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>
SQL
<SPAN class="keyword token">DECLARE</SPAN> <SPAN class="variable token">@RowCount</SPAN> <SPAN class="keyword token">int</SPAN>
<SPAN class="keyword token">SET</SPAN> <SPAN class="variable token">@RowCount</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">SELECT</SPAN> <SPAN class="token function">COUNT</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="operator token">*</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">FROM</SPAN> GISTEST<SPAN class="punctuation token">.</SPAN>VECTOR<SPAN class="punctuation token">.</SPAN>ELEMBND_old
<SPAN class="keyword token">WHERE</SPAN> GDB_TO_DATE <SPAN class="operator token">></SPAN> <SPAN class="keyword token">CONVERT</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">datetime</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'2017-06-22 11:37:33'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">20</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">DECLARE</SPAN> <SPAN class="variable token">@iterator</SPAN> <SPAN class="keyword token">INT</SPAN>
<SPAN class="keyword token">SELECT</SPAN> <SPAN class="variable token">@iterator</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token function">MIN</SPAN><SPAN class="punctuation token">(</SPAN>OBJECTID_1<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">FROM</SPAN> GISTEST<SPAN class="punctuation token">.</SPAN>VECTOR<SPAN class="punctuation token">.</SPAN>ELEMBND_OLD <SPAN class="keyword token">WHERE</SPAN> GDB_TO_DATE <SPAN class="operator token">></SPAN> <SPAN class="keyword token">CONVERT</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">datetime</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'2017-06-22 11:37:33'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">20</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">WHILE</SPAN> <SPAN class="variable token">@iterator</SPAN> <SPAN class="operator token">is</SPAN> <SPAN class="operator token">NOT</SPAN> <SPAN class="token boolean">NULL</SPAN>
<SPAN class="keyword token">BEGIN</SPAN>
<SPAN class="keyword token">DECLARE</SPAN> <SPAN class="variable token">@oid</SPAN> <SPAN class="keyword token">int</SPAN>
<SPAN class="keyword token">DECLARE</SPAN> <SPAN class="variable token">@gid</SPAN> uniqueidentifier
<SPAN class="keyword token">EXEC</SPAN> GISTEST<SPAN class="punctuation token">.</SPAN>sde<SPAN class="punctuation token">.</SPAN>next_rowid <SPAN class="string token">'VECTOR'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'ELEMBND_H'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="variable token">@oid</SPAN> OUTPUT
<SPAN class="keyword token">EXEC</SPAN> GISTEST<SPAN class="punctuation token">.</SPAN>sde<SPAN class="punctuation token">.</SPAN>next_globalid <SPAN class="variable token">@gid</SPAN> OUTPUT
<SPAN class="keyword token">INSERT</SPAN> <SPAN class="keyword token">INTO</SPAN> GISTEST<SPAN class="punctuation token">.</SPAN>VECTOR<SPAN class="punctuation token">.</SPAN>ELEMBND_H <SPAN class="punctuation token">(</SPAN>GDB_ARCHIVE_OID<SPAN class="punctuation token">,</SPAN>
GDB_TO_DATE<SPAN class="punctuation token">,</SPAN>
GLOBALID<SPAN class="punctuation token">,</SPAN>
OBJECTID<SPAN class="punctuation token">,</SPAN>
NAME<SPAN class="punctuation token">,</SPAN>
NAME_CODE<SPAN class="punctuation token">,</SPAN>
SCHLNUM<SPAN class="punctuation token">,</SPAN>
CREATED_USER<SPAN class="punctuation token">,</SPAN>
CREATED_DATE<SPAN class="punctuation token">,</SPAN>
LAST_EDITED_USER<SPAN class="punctuation token">,</SPAN>
LAST_EDITED_DATE<SPAN class="punctuation token">,</SPAN>
GDB_FROM_DATE<SPAN class="punctuation token">,</SPAN>
SHAPE<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">SELECT</SPAN> <SPAN class="variable token">@oid</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="keyword token">CONVERT</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">datetime</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'2017-06-22 11:37:33'</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">20</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="variable token">@gid</SPAN><SPAN class="punctuation token">,</SPAN>
OBJECTID<SPAN class="punctuation token">,</SPAN>
NAME<SPAN class="punctuation token">,</SPAN>
NAME_CODE<SPAN class="punctuation token">,</SPAN>
SCHLNUM<SPAN class="punctuation token">,</SPAN>
CREATED_USER<SPAN class="punctuation token">,</SPAN>
CREATED_DATE<SPAN class="punctuation token">,</SPAN>
LAST_EDITED_USER<SPAN class="punctuation token">,</SPAN>
LAST_EDITED_DATE<SPAN class="punctuation token">,</SPAN>
GDB_FROM_DATE<SPAN class="punctuation token">,</SPAN>
SHAPE
<SPAN class="keyword token">FROM</SPAN> GISTEST<SPAN class="punctuation token">.</SPAN>VECTOR<SPAN class="punctuation token">.</SPAN>ELEMBND_old
<SPAN class="keyword token">WHERE</SPAN> <SPAN class="variable token">@iterator</SPAN> <SPAN class="operator token">=</SPAN> OBJECTID_1<SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">SELECT</SPAN> <SPAN class="variable token">@iterator</SPAN> <SPAN class="operator token">=</SPAN> <SPAN class="token function">MIN</SPAN><SPAN class="punctuation token">(</SPAN>OBJECTID_1<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">FROM</SPAN> GISTEST<SPAN class="punctuation token">.</SPAN>VECTOR<SPAN class="punctuation token">.</SPAN>ELEMBND_OLD <SPAN class="keyword token">WHERE</SPAN> GDB_TO_DATE <SPAN class="operator token">></SPAN> <SPAN class="keyword token">CONVERT</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="keyword token">datetime</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'2017-06-22 11:37:33'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">20</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">AND</SPAN> <SPAN class="variable token">@iterator</SPAN> <SPAN class="operator token"><</SPAN> OBJECTID_1<SPAN class="punctuation token">;</SPAN>
<SPAN class="keyword token">END</SPAN>
<SPAN class="keyword token">select</SPAN> <SPAN class="operator token">*</SPAN> <SPAN class="keyword token">from</SPAN> GISTEST<SPAN class="punctuation token">.</SPAN>VECTOR<SPAN class="punctuation token">.</SPAN>ELEMBND_H<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></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><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>