<?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 Is there a way to use temporary tables with arcpy query layer functions? in ArcGIS API for Python Questions</title>
    <link>https://community.esri.com/t5/arcgis-api-for-python-questions/is-there-a-way-to-use-temporary-tables-with-arcpy/m-p/1016732#M5423</link>
    <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I would like to create feature layers from data in temporary tables. I have tried the following:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;database_connection = r"C:\project\database.sde"
create = "CREATE TEMP TABLE temp_example(object_id int, name varchar(80))"
insert = "INSERT INTO temp_example VALUES (1, 'wtf')"
select_temp = "SELECT * FROM temp_example"
select_real = "SELECT report_id, report_type FROM report LIMIT 1"

arc_sde = arcpy.ArcSDESQLExecute(database_connection)
arc_sde.execute(create)
arc_sde.execute(insert)
result = arc_sde.execute(select_temp)
for row in result:
    print(row)

# this works
table = arcpy.MakeQueryLayer_management(
    input_database      = database_connection,
    out_layer_name      = "Real_Table",
    query               = select_real,
    oid_fields          = "report_id"
)[0]

# this fails
table = arcpy.MakeQueryLayer_management(
    input_database      = database_connection,
    out_layer_name      = "Temp_Table",
    query               = select_temp,
    oid_fields          = "object_id"
)[0]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This returns the classic Error 999999. I suspect ArcSDESQLExecute is creating a separate connection, meaning the tables don't exist in the original database_connection. I have tried passing arc_sde to MakeQueryLayer as the database but then I get the following error:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;RuntimeError: ArcSDESQLExecute: Get attribute __class__ does not exist&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have also tried using psycopg2 to create the temporary tables, but still get Error 999999 when trying to use them with arcpy.&lt;/P&gt;&lt;P&gt;Is there another way to use temporary tables with MakeQueryLayer and other arcpy functions?&lt;/P&gt;&lt;P&gt;I am using ArcGIS Pro 2.7 with the Basic license, and the database is PostgreSQL 12.2 with PostGIS 3.01.&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
    <pubDate>Thu, 14 Jan 2021 02:10:10 GMT</pubDate>
    <dc:creator>JesseTemplin2</dc:creator>
    <dc:date>2021-01-14T02:10:10Z</dc:date>
    <item>
      <title>Is there a way to use temporary tables with arcpy query layer functions?</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/is-there-a-way-to-use-temporary-tables-with-arcpy/m-p/1016732#M5423</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I would like to create feature layers from data in temporary tables. I have tried the following:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;database_connection = r"C:\project\database.sde"
create = "CREATE TEMP TABLE temp_example(object_id int, name varchar(80))"
insert = "INSERT INTO temp_example VALUES (1, 'wtf')"
select_temp = "SELECT * FROM temp_example"
select_real = "SELECT report_id, report_type FROM report LIMIT 1"

arc_sde = arcpy.ArcSDESQLExecute(database_connection)
arc_sde.execute(create)
arc_sde.execute(insert)
result = arc_sde.execute(select_temp)
for row in result:
    print(row)

# this works
table = arcpy.MakeQueryLayer_management(
    input_database      = database_connection,
    out_layer_name      = "Real_Table",
    query               = select_real,
    oid_fields          = "report_id"
)[0]

# this fails
table = arcpy.MakeQueryLayer_management(
    input_database      = database_connection,
    out_layer_name      = "Temp_Table",
    query               = select_temp,
    oid_fields          = "object_id"
)[0]&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This returns the classic Error 999999. I suspect ArcSDESQLExecute is creating a separate connection, meaning the tables don't exist in the original database_connection. I have tried passing arc_sde to MakeQueryLayer as the database but then I get the following error:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;RuntimeError: ArcSDESQLExecute: Get attribute __class__ does not exist&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have also tried using psycopg2 to create the temporary tables, but still get Error 999999 when trying to use them with arcpy.&lt;/P&gt;&lt;P&gt;Is there another way to use temporary tables with MakeQueryLayer and other arcpy functions?&lt;/P&gt;&lt;P&gt;I am using ArcGIS Pro 2.7 with the Basic license, and the database is PostgreSQL 12.2 with PostGIS 3.01.&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jan 2021 02:10:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/is-there-a-way-to-use-temporary-tables-with-arcpy/m-p/1016732#M5423</guid>
      <dc:creator>JesseTemplin2</dc:creator>
      <dc:date>2021-01-14T02:10:10Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to use temporary tables with arcpy query layer functions?</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/is-there-a-way-to-use-temporary-tables-with-arcpy/m-p/1016764#M5425</link>
      <description>&lt;P&gt;I feel it's something to do the OBJECTID&lt;/P&gt;&lt;P&gt;Can you try this:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;create = "CREATE TEMP TABLE temp_example(OBJECTID Long, name varchar(80))"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have no experience with PostgreSQL and PostGIS, so it's a long shot&lt;span class="lia-unicode-emoji" title=":grinning_face:"&gt;😀&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jan 2021 06:36:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/is-there-a-way-to-use-temporary-tables-with-arcpy/m-p/1016764#M5425</guid>
      <dc:creator>simoxu</dc:creator>
      <dc:date>2021-01-14T06:36:13Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to use temporary tables with arcpy query layer functions?</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/is-there-a-way-to-use-temporary-tables-with-arcpy/m-p/1017018#M5427</link>
      <description>&lt;P&gt;Long isn't a valid Postgres type, and if I use "OBJECTID int" I get the same 999999 error.&lt;/P&gt;&lt;P&gt;Thanks for the suggestion though.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jan 2021 19:24:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/is-there-a-way-to-use-temporary-tables-with-arcpy/m-p/1017018#M5427</guid>
      <dc:creator>JesseTemplin2</dc:creator>
      <dc:date>2021-01-14T19:24:10Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to use temporary tables with arcpy query layer functions?</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/is-there-a-way-to-use-temporary-tables-with-arcpy/m-p/1017042#M5428</link>
      <description>&lt;P&gt;Just for kicks, try:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;select_temp = "SELECT object_id, name FROM temp_example"&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 14 Jan 2021 20:27:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/is-there-a-way-to-use-temporary-tables-with-arcpy/m-p/1017042#M5428</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2021-01-14T20:27:40Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a way to use temporary tables with arcpy query layer functions?</title>
      <link>https://community.esri.com/t5/arcgis-api-for-python-questions/is-there-a-way-to-use-temporary-tables-with-arcpy/m-p/1017044#M5429</link>
      <description>&lt;P&gt;Same result, but thanks for the suggestion. After looking at the database connections while running these functions, it's clear that ArcSDESQLExecute is creating a separate connection, which explains why the temporary tables aren't visible to MakeQueryLayer.&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jan 2021 20:35:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-api-for-python-questions/is-there-a-way-to-use-temporary-tables-with-arcpy/m-p/1017044#M5429</guid>
      <dc:creator>JesseTemplin2</dc:creator>
      <dc:date>2021-01-14T20:35:08Z</dc:date>
    </item>
  </channel>
</rss>

