<?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 code for appending a notebook result into an existing database table in Community Articles</title>
    <link>https://community.databricks.com/t5/community-articles/sql-code-for-appending-a-notebook-result-into-an-existing/m-p/89364#M260</link>
    <description>&lt;P&gt;I am attempting to append the results from a notebook query results table into an existing databricks database table.&amp;nbsp; By chance would someone share an example of the sql code with me?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 10 Sep 2024 20:02:30 GMT</pubDate>
    <dc:creator>wheersink</dc:creator>
    <dc:date>2024-09-10T20:02:30Z</dc:date>
    <item>
      <title>SQL code for appending a notebook result into an existing database table</title>
      <link>https://community.databricks.com/t5/community-articles/sql-code-for-appending-a-notebook-result-into-an-existing/m-p/89364#M260</link>
      <description>&lt;P&gt;I am attempting to append the results from a notebook query results table into an existing databricks database table.&amp;nbsp; By chance would someone share an example of the sql code with me?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Sep 2024 20:02:30 GMT</pubDate>
      <guid>https://community.databricks.com/t5/community-articles/sql-code-for-appending-a-notebook-result-into-an-existing/m-p/89364#M260</guid>
      <dc:creator>wheersink</dc:creator>
      <dc:date>2024-09-10T20:02:30Z</dc:date>
    </item>
    <item>
      <title>Re: SQL code for appending a notebook result into an existing database table</title>
      <link>https://community.databricks.com/t5/community-articles/sql-code-for-appending-a-notebook-result-into-an-existing/m-p/89424#M261</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/120186"&gt;@wheersink&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;So let's say you created following table with some sample values.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;%sql
CREATE TABLE dev.default.employee (
  id INT,
  name STRING,
  age INT,
  department STRING
);

INSERT INTO dev.default.employee VALUES
  (1, 'John Doe', 30, 'Finance'),
  (2, 'Jane Smith', 35, 'HR'),
  (3, 'Michael Johnson', 28, 'IT');&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;If you want to insert to this table additional rows that are result of some query you can use following approach:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Define the SQL query to select new employees
new_employees_sql = """
SELECT 4 AS id, 'Emma Green' AS name, 27 AS age, 'Marketing' AS department
UNION ALL
SELECT 5, 'Tom Blue', 40, 'Operations'
"""

# Execute the SQL query and get the result as a DataFrame
new_employees_df = spark.sql(new_employees_sql)

# Insert the result into the employee table
new_employees_df.write.insertInto("dev.default.employee", overwrite=False)&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="szymon_dybczak_0-1726043895434.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/11068i209858EB21E49EB2/image-size/medium?v=v2&amp;amp;px=400" role="button" title="szymon_dybczak_0-1726043895434.png" alt="szymon_dybczak_0-1726043895434.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;You can also use pure sql to perform this action:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;%sql
-- Create a query to select employees for the new insert
WITH new_employees AS (
  SELECT 6 AS id, 'Alice Brown' AS name, 32 AS age, 'Engineering' AS department
  UNION ALL
  SELECT 7, 'Robert White', 29, 'Engineering'
)

-- Insert the results of the query into the existing employee table
INSERT INTO dev.default.employee
SELECT * FROM new_employees;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="szymon_dybczak_1-1726044003975.png" style="width: 400px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/11069i6A42EE00B3A0C62A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="szymon_dybczak_1-1726044003975.png" alt="szymon_dybczak_1-1726044003975.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Sep 2024 08:40:10 GMT</pubDate>
      <guid>https://community.databricks.com/t5/community-articles/sql-code-for-appending-a-notebook-result-into-an-existing/m-p/89424#M261</guid>
      <dc:creator>szymon_dybczak</dc:creator>
      <dc:date>2024-09-11T08:40:10Z</dc:date>
    </item>
  </channel>
</rss>

