<?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: Assistance with Capturing Auto-Generated IDs in Databricks SQL in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/assistance-with-capturing-auto-generated-ids-in-databricks-sql/m-p/122179#M46680</link>
    <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/130106"&gt;@agallard&lt;/a&gt;&amp;nbsp;I am also wondering about the alternatives - could you share these?&lt;/P&gt;</description>
    <pubDate>Wed, 18 Jun 2025 17:56:06 GMT</pubDate>
    <dc:creator>Megan123</dc:creator>
    <dc:date>2025-06-18T17:56:06Z</dc:date>
    <item>
      <title>Assistance with Capturing Auto-Generated IDs in Databricks SQL</title>
      <link>https://community.databricks.com/t5/data-engineering/assistance-with-capturing-auto-generated-ids-in-databricks-sql/m-p/99921#M40141</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I am currently working on a project where I need to insert multiple rows into a table and capture the auto-generated IDs for each row. I am using databricks sql connector.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a simplified version of my current workflow:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;I create a temporary view from a DataFrame.&lt;/LI&gt;&lt;LI&gt;I insert data from this temporary view into a target table.&lt;/LI&gt;&lt;LI&gt;I need to capture the auto-generated IDs (e.g.,&lt;SPAN&gt; id_col&lt;/SPAN&gt;) for each inserted row to log additional information in another table.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;I have tried a `returning` statement, which would work perfectly, however, it's not available.&amp;nbsp;&lt;/P&gt;&lt;P&gt;`&lt;SPAN&gt;id_col&lt;/SPAN&gt;` column is auto-generated upon data insertion.&amp;nbsp;&lt;/P&gt;&lt;P&gt;# creating a temporary table (using local notebook, this helps pass off compute to databricks and return only the results)&lt;/P&gt;&lt;P&gt;data_tuples = [tuple(x) for x in df.to_numpy()]&lt;BR /&gt;create_view_sql = "create or replace temporary view my_temp_view as select * from values"&lt;BR /&gt;values_str = ", ".join([f"({', '.join([format_value2(item) for item in row])})" for row in data_tuples])&lt;BR /&gt;column_names_str = ", ".join(df.columns.tolist())&lt;BR /&gt;create_view_sql += values_str + f" as t({column_names_str})"&lt;BR /&gt;cursor = conn.cursor()&lt;BR /&gt;cursor.execute(create_view_sql)&lt;/P&gt;&lt;P&gt;# this is where I need help - the returning functionality doesn't exist for databricks&amp;nbsp;&lt;/P&gt;&lt;P&gt;query = """&lt;BR /&gt;insert into my_table (col2, col3)&lt;BR /&gt;select col2, col3&lt;BR /&gt;from my_temp_view&lt;BR /&gt;returning&amp;nbsp;&lt;SPAN&gt;id_col&lt;/SPAN&gt;&lt;BR /&gt;"""&lt;BR /&gt;&lt;BR /&gt;cursor.execute(query)&lt;BR /&gt;new_ids = cursor.fetchall()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks for your help and time!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 25 Nov 2024 03:15:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/assistance-with-capturing-auto-generated-ids-in-databricks-sql/m-p/99921#M40141</guid>
      <dc:creator>vanverne</dc:creator>
      <dc:date>2024-11-25T03:15:34Z</dc:date>
    </item>
    <item>
      <title>Re: Assistance with Capturing Auto-Generated IDs in Databricks SQL</title>
      <link>https://community.databricks.com/t5/data-engineering/assistance-with-capturing-auto-generated-ids-in-databricks-sql/m-p/99945#M40153</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/133616"&gt;@vanverne&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Unfortunately, as of now, Databricks SQL does not support the RETURNING clause directly when inserting rows into a table. This limitation makes it tricky to capture auto-generated IDs during an INSERT operation.&lt;/P&gt;&lt;P&gt;However, you can achieve the desired functionality by using a combination of temporary tables, merge queries, or other approaches. Below are a few alternatives to help you capture the auto-generated IDs for your use case.&lt;/P&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;P&gt;Let me know if you need further clarification or assistance!&lt;/P&gt;&lt;P&gt;Regards!&lt;/P&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;DIV class=""&gt;&lt;DIV&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;</description>
      <pubDate>Mon, 25 Nov 2024 12:12:49 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/assistance-with-capturing-auto-generated-ids-in-databricks-sql/m-p/99945#M40153</guid>
      <dc:creator>agallard</dc:creator>
      <dc:date>2024-11-25T12:12:49Z</dc:date>
    </item>
    <item>
      <title>Re: Assistance with Capturing Auto-Generated IDs in Databricks SQL</title>
      <link>https://community.databricks.com/t5/data-engineering/assistance-with-capturing-auto-generated-ids-in-databricks-sql/m-p/101137#M40555</link>
      <description>&lt;P&gt;Thanks for the reply, Alfonso. I noticed you mentioned "Below are a few alternatives...", however, I am not seeing those. Please let me know if I am missing something. Also, do you know if Databricks is working on supporting the RETURNING clause soon? If not, could we open a case to add that functionality?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 05 Dec 2024 22:09:50 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/assistance-with-capturing-auto-generated-ids-in-databricks-sql/m-p/101137#M40555</guid>
      <dc:creator>vanverne</dc:creator>
      <dc:date>2024-12-05T22:09:50Z</dc:date>
    </item>
    <item>
      <title>Re: Assistance with Capturing Auto-Generated IDs in Databricks SQL</title>
      <link>https://community.databricks.com/t5/data-engineering/assistance-with-capturing-auto-generated-ids-in-databricks-sql/m-p/122179#M46680</link>
      <description>&lt;P&gt;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/130106"&gt;@agallard&lt;/a&gt;&amp;nbsp;I am also wondering about the alternatives - could you share these?&lt;/P&gt;</description>
      <pubDate>Wed, 18 Jun 2025 17:56:06 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/assistance-with-capturing-auto-generated-ids-in-databricks-sql/m-p/122179#M46680</guid>
      <dc:creator>Megan123</dc:creator>
      <dc:date>2025-06-18T17:56:06Z</dc:date>
    </item>
  </channel>
</rss>

