<?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 Notebook Merge Only Inserting Not Updating Matched Rows in Data Engineering</title>
    <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Notebook-Merge-Only-Inserting-Not-Updating-Matched-Rows/m-p/4787301#M11486</link>
    <description>&lt;P&gt;See my code below:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;from delta.tables import *

# Get the target Delta table.
target_table = DeltaTable.forName(spark, "targettable")

# Define the source DataFrame (e.g., from a CSV file or another table).
source_table_path = "abfss://stagetable"
source_table = spark.read.format("delta").load(source_table_path)

# Run the merge operation.
(
    target_table.alias("target")
    .merge(
        source_table.alias("source"),
        condition="target.id = source.id AND target.transaction = source.transaction AND target.createdate = source.createdate"  # Replace with your matching condition
    )
    .whenMatchedUpdateAll()  # Update all columns if matched
    .whenNotMatchedInsertAll() # Insert if not matched
    .execute()
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As expected when I run my code the first time without any records in the table that are in my stage table, it inserts the records.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The second time, when run the code, it is inserting the same records again, which to me seems like it is ignoring the condition clause because it should find matches for what it already inserted.&amp;nbsp; I'm struggling to understand why it is doing it.&amp;nbsp; Any help would be appreciated.&lt;/P&gt;</description>
    <pubDate>Tue, 05 Aug 2025 01:25:54 GMT</pubDate>
    <dc:creator>gdb729</dc:creator>
    <dc:date>2025-08-05T01:25:54Z</dc:date>
    <item>
      <title>Notebook Merge Only Inserting Not Updating Matched Rows</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Notebook-Merge-Only-Inserting-Not-Updating-Matched-Rows/m-p/4787301#M11486</link>
      <description>&lt;P&gt;See my code below:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;from delta.tables import *

# Get the target Delta table.
target_table = DeltaTable.forName(spark, "targettable")

# Define the source DataFrame (e.g., from a CSV file or another table).
source_table_path = "abfss://stagetable"
source_table = spark.read.format("delta").load(source_table_path)

# Run the merge operation.
(
    target_table.alias("target")
    .merge(
        source_table.alias("source"),
        condition="target.id = source.id AND target.transaction = source.transaction AND target.createdate = source.createdate"  # Replace with your matching condition
    )
    .whenMatchedUpdateAll()  # Update all columns if matched
    .whenNotMatchedInsertAll() # Insert if not matched
    .execute()
)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As expected when I run my code the first time without any records in the table that are in my stage table, it inserts the records.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The second time, when run the code, it is inserting the same records again, which to me seems like it is ignoring the condition clause because it should find matches for what it already inserted.&amp;nbsp; I'm struggling to understand why it is doing it.&amp;nbsp; Any help would be appreciated.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Aug 2025 01:25:54 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Notebook-Merge-Only-Inserting-Not-Updating-Matched-Rows/m-p/4787301#M11486</guid>
      <dc:creator>gdb729</dc:creator>
      <dc:date>2025-08-05T01:25:54Z</dc:date>
    </item>
    <item>
      <title>Re: Notebook Merge Only Inserting Not Updating Matched Rows</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Notebook-Merge-Only-Inserting-Not-Updating-Matched-Rows/m-p/4787803#M11500</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/1258224"&gt;@gdb729&lt;/a&gt;&amp;nbsp;,&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;P&gt;It sounds like you're really close but what you're describing usually points to the merge condition not evaluating as a true match, even though the data looks identical at first glance. A few things that can trip this up.&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Data type mismatches: For example, if target.id is an int and source.id is a string, Spark won't match them even if the values appear the same.&lt;/LI&gt;
&lt;LI&gt;Null values: Regular = comparison fails when either side is null. In Spark, null = null returns false. You’ll want to use the null-safe equality operator (&amp;lt;=&amp;gt;) instead.&lt;/LI&gt;
&lt;LI&gt;Whitespace / case issues:&amp;nbsp; &amp;nbsp;Especially on strings like transaction, even a trailing space can cause a mismatch.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Regards,&lt;BR /&gt;Akhil.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Aug 2025 11:20:34 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Notebook-Merge-Only-Inserting-Not-Updating-Matched-Rows/m-p/4787803#M11500</guid>
      <dc:creator>v-agajavelly</dc:creator>
      <dc:date>2025-08-05T11:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: Notebook Merge Only Inserting Not Updating Matched Rows</title>
      <link>https://community.fabric.microsoft.com/t5/Data-Engineering/Notebook-Merge-Only-Inserting-Not-Updating-Matched-Rows/m-p/4788229#M11511</link>
      <description>&lt;P&gt;Thanks for the quick response.&amp;nbsp; Ended up being a null filter and when I swapped the equality operator which I didn't think I needed as that field shouldn't be null, merge worked correctly.&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Aug 2025 18:50:22 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Data-Engineering/Notebook-Merge-Only-Inserting-Not-Updating-Matched-Rows/m-p/4788229#M11511</guid>
      <dc:creator>gdb729</dc:creator>
      <dc:date>2025-08-05T18:50:22Z</dc:date>
    </item>
  </channel>
</rss>

