<?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: Unable to overwrite table to Azure sql db in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/unable-to-overwrite-table-to-azure-sql-db/m-p/65175#M32749</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/103050"&gt;@Kingston&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Make sure that you have the proper permissions on the SQL server for the user you do the authentication through JDBC with, i.e. database reader / database writer. Then your approach can go in two directions, push the data from Databricks --&amp;gt; SQL server and trigger the processing there, by running an SP from Databricks side, or the better option would be to pull the data from the SQL server, transform in Databricks and push back to the SQL server, truncating the original table in the process. Below is an example code, let me know if this helps.&lt;/P&gt;&lt;P&gt;from pyspark.sql import SparkSession&lt;BR /&gt;from pyspark.sql.functions import *&lt;/P&gt;&lt;P&gt;# Initialize SparkSession&lt;BR /&gt;spark = SparkSession.builder \&lt;BR /&gt;.appName("PySpark SQL Server Example") \&lt;BR /&gt;.getOrCreate()&lt;/P&gt;&lt;P&gt;# SQL Server JDBC URL&lt;BR /&gt;url = "jdbc:sqlserver://&amp;lt;server_name&amp;gt;:&amp;lt;port&amp;gt;;databaseName=&amp;lt;database_name&amp;gt;"&lt;BR /&gt;properties = {&lt;BR /&gt;"user": "&amp;lt;username&amp;gt;",&lt;BR /&gt;"password": "&amp;lt;password&amp;gt;",&lt;BR /&gt;"driver": "com.microsoft.sqlserver.jdbc.SQLServerDriver"&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;# Assuming `df` is your DataFrame that you want to merge into the SQL Server table&lt;/P&gt;&lt;P&gt;# Load the target table from SQL Server into a DataFrame&lt;BR /&gt;tbl1_df = spark.read.jdbc(url=url, table="tbl1", properties=properties)&lt;/P&gt;&lt;P&gt;# Perform the merge operation. This is a simplistic approach.&lt;BR /&gt;# You would need to customize this logic based on your actual merge/upsert rules.&lt;BR /&gt;merged_df = df.union(tbl1_df).distinct()&lt;/P&gt;&lt;P&gt;# Write the merged DataFrame back to the SQL Server table, overwriting the existing data.&lt;BR /&gt;# Note: This will replace the table content. If you need a more nuanced merge strategy,&lt;BR /&gt;# you might need to perform the merge logic directly in SQL Server.&lt;BR /&gt;merged_df.write.jdbc(url=url, table="tbl1", mode="overwrite", properties=properties)&lt;/P&gt;</description>
    <pubDate>Mon, 01 Apr 2024 11:00:45 GMT</pubDate>
    <dc:creator>YuliyanBogdanov</dc:creator>
    <dc:date>2024-04-01T11:00:45Z</dc:date>
    <item>
      <title>Unable to overwrite table to Azure sql db</title>
      <link>https://community.databricks.com/t5/data-engineering/unable-to-overwrite-table-to-azure-sql-db/m-p/65109#M32739</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a requirement to read table from azure sql db and update the table in azure databricks with transformations and overwrite updated table to the azure sql db but due to lazy evaluation of pyspark im unable to overwrite the table in azure sql db it just truncating the table not replacing&lt;/P&gt;</description>
      <pubDate>Sun, 31 Mar 2024 04:41:28 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/unable-to-overwrite-table-to-azure-sql-db/m-p/65109#M32739</guid>
      <dc:creator>Kingston</dc:creator>
      <dc:date>2024-03-31T04:41:28Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to overwrite table to Azure sql db</title>
      <link>https://community.databricks.com/t5/data-engineering/unable-to-overwrite-table-to-azure-sql-db/m-p/65110#M32740</link>
      <description>&lt;P&gt;Using below code for reference&lt;/P&gt;</description>
      <pubDate>Sun, 31 Mar 2024 04:50:23 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/unable-to-overwrite-table-to-azure-sql-db/m-p/65110#M32740</guid>
      <dc:creator>Kingston</dc:creator>
      <dc:date>2024-03-31T04:50:23Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to overwrite table to Azure sql db</title>
      <link>https://community.databricks.com/t5/data-engineering/unable-to-overwrite-table-to-azure-sql-db/m-p/65111#M32741</link>
      <description />
      <pubDate>Sun, 31 Mar 2024 04:55:50 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/unable-to-overwrite-table-to-azure-sql-db/m-p/65111#M32741</guid>
      <dc:creator>Kingston</dc:creator>
      <dc:date>2024-03-31T04:55:50Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to overwrite table to Azure sql db</title>
      <link>https://community.databricks.com/t5/data-engineering/unable-to-overwrite-table-to-azure-sql-db/m-p/65175#M32749</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/103050"&gt;@Kingston&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Make sure that you have the proper permissions on the SQL server for the user you do the authentication through JDBC with, i.e. database reader / database writer. Then your approach can go in two directions, push the data from Databricks --&amp;gt; SQL server and trigger the processing there, by running an SP from Databricks side, or the better option would be to pull the data from the SQL server, transform in Databricks and push back to the SQL server, truncating the original table in the process. Below is an example code, let me know if this helps.&lt;/P&gt;&lt;P&gt;from pyspark.sql import SparkSession&lt;BR /&gt;from pyspark.sql.functions import *&lt;/P&gt;&lt;P&gt;# Initialize SparkSession&lt;BR /&gt;spark = SparkSession.builder \&lt;BR /&gt;.appName("PySpark SQL Server Example") \&lt;BR /&gt;.getOrCreate()&lt;/P&gt;&lt;P&gt;# SQL Server JDBC URL&lt;BR /&gt;url = "jdbc:sqlserver://&amp;lt;server_name&amp;gt;:&amp;lt;port&amp;gt;;databaseName=&amp;lt;database_name&amp;gt;"&lt;BR /&gt;properties = {&lt;BR /&gt;"user": "&amp;lt;username&amp;gt;",&lt;BR /&gt;"password": "&amp;lt;password&amp;gt;",&lt;BR /&gt;"driver": "com.microsoft.sqlserver.jdbc.SQLServerDriver"&lt;BR /&gt;}&lt;/P&gt;&lt;P&gt;# Assuming `df` is your DataFrame that you want to merge into the SQL Server table&lt;/P&gt;&lt;P&gt;# Load the target table from SQL Server into a DataFrame&lt;BR /&gt;tbl1_df = spark.read.jdbc(url=url, table="tbl1", properties=properties)&lt;/P&gt;&lt;P&gt;# Perform the merge operation. This is a simplistic approach.&lt;BR /&gt;# You would need to customize this logic based on your actual merge/upsert rules.&lt;BR /&gt;merged_df = df.union(tbl1_df).distinct()&lt;/P&gt;&lt;P&gt;# Write the merged DataFrame back to the SQL Server table, overwriting the existing data.&lt;BR /&gt;# Note: This will replace the table content. If you need a more nuanced merge strategy,&lt;BR /&gt;# you might need to perform the merge logic directly in SQL Server.&lt;BR /&gt;merged_df.write.jdbc(url=url, table="tbl1", mode="overwrite", properties=properties)&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2024 11:00:45 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/unable-to-overwrite-table-to-azure-sql-db/m-p/65175#M32749</guid>
      <dc:creator>YuliyanBogdanov</dc:creator>
      <dc:date>2024-04-01T11:00:45Z</dc:date>
    </item>
  </channel>
</rss>

