<?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 SQL works in python console but not when run as a geoprocessing tool in ArcGIS Pro Questions</title>
    <link>https://community.esri.com/t5/arcgis-pro-questions/sql-works-in-python-console-but-not-when-run-as-a/m-p/1171807#M54896</link>
    <description>&lt;P&gt;I'm in the process of transferring all the arcpy scripts I've written to geoprocessing tools so that my team can run them without my hand-holding and it's *mostly* working out ok but I'm running into snags here and there and my current one has to do with the geoprocessing tool complaining about an invalid SQL expression. It throws "&lt;SPAN class=""&gt;arcgisscripting.ExecuteError: &lt;/SPAN&gt;ERROR 000358:&lt;SPAN class=""&gt; Invalid expression" when it gets to the SelectLayerByAttributes portion,&amp;nbsp;&lt;EM&gt;an issue that does not happen when run from the python console.&lt;/EM&gt; my entire script runs just fine via console, but breaks when run as a GP tool.&amp;nbsp; The temp layer, btw, was created by an in_memory feature class if that's relevant.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    with arcpy.da.SearchCursor(temp_worklocation_layer, "OID@") as locations:
        for point in locations:
            cur_point_sql = f"OBJECTID={point[0]}"
            arcpy.management.SelectLayerByAttribute(temp_worklocation_layer, "CLEAR_SELECTION")
            arcpy.SelectLayerByAttribute_management(temp_worklocation_layer, 'NEW_SELECTION', cur_point_sql)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do GP tools handle SQL queries in a slightly different format than whatever handles it in the python console? Why does "OBJECTID = #" work in one but not the other?&lt;BR /&gt;&lt;BR /&gt;Also slightly related, but this method of grabbing multiple features and then iterating back through them with the OID one at a time and doing a SelectLayerByLocation to grab other layers' features nearby is something I do numerous times across my scripts. Is that the most efficient way? Am I doing something redundant?&lt;/P&gt;</description>
    <pubDate>Sun, 08 May 2022 18:38:06 GMT</pubDate>
    <dc:creator>Glasnoct</dc:creator>
    <dc:date>2022-05-08T18:38:06Z</dc:date>
    <item>
      <title>SQL works in python console but not when run as a geoprocessing tool</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/sql-works-in-python-console-but-not-when-run-as-a/m-p/1171807#M54896</link>
      <description>&lt;P&gt;I'm in the process of transferring all the arcpy scripts I've written to geoprocessing tools so that my team can run them without my hand-holding and it's *mostly* working out ok but I'm running into snags here and there and my current one has to do with the geoprocessing tool complaining about an invalid SQL expression. It throws "&lt;SPAN class=""&gt;arcgisscripting.ExecuteError: &lt;/SPAN&gt;ERROR 000358:&lt;SPAN class=""&gt; Invalid expression" when it gets to the SelectLayerByAttributes portion,&amp;nbsp;&lt;EM&gt;an issue that does not happen when run from the python console.&lt;/EM&gt; my entire script runs just fine via console, but breaks when run as a GP tool.&amp;nbsp; The temp layer, btw, was created by an in_memory feature class if that's relevant.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    with arcpy.da.SearchCursor(temp_worklocation_layer, "OID@") as locations:
        for point in locations:
            cur_point_sql = f"OBJECTID={point[0]}"
            arcpy.management.SelectLayerByAttribute(temp_worklocation_layer, "CLEAR_SELECTION")
            arcpy.SelectLayerByAttribute_management(temp_worklocation_layer, 'NEW_SELECTION', cur_point_sql)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do GP tools handle SQL queries in a slightly different format than whatever handles it in the python console? Why does "OBJECTID = #" work in one but not the other?&lt;BR /&gt;&lt;BR /&gt;Also slightly related, but this method of grabbing multiple features and then iterating back through them with the OID one at a time and doing a SelectLayerByLocation to grab other layers' features nearby is something I do numerous times across my scripts. Is that the most efficient way? Am I doing something redundant?&lt;/P&gt;</description>
      <pubDate>Sun, 08 May 2022 18:38:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/sql-works-in-python-console-but-not-when-run-as-a/m-p/1171807#M54896</guid>
      <dc:creator>Glasnoct</dc:creator>
      <dc:date>2022-05-08T18:38:06Z</dc:date>
    </item>
    <item>
      <title>Re: SQL works in python console but not when run as a geoprocessing tool</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/sql-works-in-python-console-but-not-when-run-as-a/m-p/1171809#M54897</link>
      <description>&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/select-layer-by-attribute.htm" target="_blank"&gt;Select Layer By Attribute (Data Management)—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;"[NAME] = 'California'"&lt;/P&gt;&lt;P&gt;'"population" &amp;gt; 10000'&lt;/P&gt;&lt;P&gt;just to add to the confusion...&lt;/P&gt;&lt;P&gt;but the single and double quotes appear to be common so&lt;/P&gt;&lt;P&gt;"OBJECTID = #"&amp;nbsp; may be '"OBJECTID" = #'&lt;/P&gt;&lt;P&gt;best bet is to run the tool and copy as a python snippet&lt;/P&gt;</description>
      <pubDate>Sun, 08 May 2022 19:26:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/sql-works-in-python-console-but-not-when-run-as-a/m-p/1171809#M54897</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2022-05-08T19:26:46Z</dc:date>
    </item>
    <item>
      <title>Re: SQL works in python console but not when run as a geoprocessing tool</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/sql-works-in-python-console-but-not-when-run-as-a/m-p/1171822#M54899</link>
      <description>&lt;P&gt;I solved my issue, but I'm not really sure&amp;nbsp;&lt;EM&gt;how&lt;/EM&gt; I did so. I have a little all-in-one function that puts a temporary in_memory layer on the map. When I changed 'in_memory' to 'memory', the problem went away.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def gen_inmem_maplayer(name, fctype, spatialreflyrname):  # layer name, layer type [str], layer for spatial reference
    inmem_fc_path = arcpy.management.CreateFeatureclass('in_memory', name, fctype, '', '', '', spatialreflyrname)[0]
    inmem_lyr_path = arcpy.MakeFeatureLayer_management(inmem_fc_path, name)[0]
    mxd.addLayer(inmem_lyr_path, 'TOP')&lt;/LI-CODE&gt;&lt;P&gt;&lt;BR /&gt;What I find confusing is that in the process of trying to work out a solution, the script would run fine as a GP tool if I joined all the relevant pieces of code into a single script file instead of referencing functions from my other modules. Originally my GP tool looked something like this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;GP tool script--------------------------------------
import database_Functions # another .py file
import engineering_units  # another .py file

engineering_units.somefunction(args)
    - this function calls database_functions.gen_inmem_maplayer as seen above
    - then attempts to run the problematic loop with the SQL query
    - script fails at this point complaining about the SQL query&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if instead I run a GP tool script that looks like this, I don't get the query complaint&lt;/P&gt;&lt;LI-CODE lang="c"&gt;GP tool script--------------------------------------
import database_Functions # another .py file
import engineering_units  # another .py file

database_functions.gen_inmem_maplayer
&amp;lt;problematic loop with the SQL query from engineering_units.somefunction()&amp;gt;
&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My guess is that once&amp;nbsp;gen_inmem_maplayer finishes, it clears anything written to in_memory and thus engineering_units script cannot locate it. This might explain the exclamation point that appears next to the map layer almost immediately after the layer is added to the map and&amp;nbsp;gen_inmem_maplayer completes.&lt;/P&gt;</description>
      <pubDate>Sun, 08 May 2022 23:03:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/sql-works-in-python-console-but-not-when-run-as-a/m-p/1171822#M54899</guid>
      <dc:creator>Glasnoct</dc:creator>
      <dc:date>2022-05-08T23:03:41Z</dc:date>
    </item>
    <item>
      <title>Re: SQL works in python console but not when run as a geoprocessing tool</title>
      <link>https://community.esri.com/t5/arcgis-pro-questions/sql-works-in-python-console-but-not-when-run-as-a/m-p/1171825#M54900</link>
      <description>&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/tool-reference/appendices/using-the-in-memory-output-workspace.htm" target="_blank"&gt;Considerations when using the in_memory workspace—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/P&gt;&lt;P&gt;has limits on what it can use as inputs.&lt;/P&gt;&lt;P&gt;intermediate results will vanish unless they are saved to disk, which I suspect might be part of the problem.&lt;/P&gt;&lt;P&gt;In any event, the scratch geodatabase and Delete_management do provide similar functionality especially if one's machine is memory limited&lt;/P&gt;</description>
      <pubDate>Sun, 08 May 2022 23:21:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-pro-questions/sql-works-in-python-console-but-not-when-run-as-a/m-p/1171825#M54900</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2022-05-08T23:21:55Z</dc:date>
    </item>
  </channel>
</rss>

