title: Check if inserted and deleted tables are identical in SQL Server trigger
body: <p>I wrote a trigger to keep a history of updates in a table. It is working well. However, I noticed sometimes the table is updated with the same exact values. This effectively does not change the table but fires the trigger, resulting in duplicate rows in my history table.</p>
  
  <p>To solve this, I'd like to only execute the body of my trigger when the <code>inserted</code> and <code>deleted</code> tables are not identical.</p>
  
  <p>Here is my current trigger. It consists of a MERGE and an INSERT. If the <code>inserted</code> and <code>deleted</code> tables are identical, I don't want to MERGE nor INSERT:</p>
  
  <pre><code>BEGIN
      DECLARE @yesterday date, @maxDate date
      SET @yesterday = DATEADD(DD, -1, GETDATE())
      SET @maxDate = '9999-12-31'
  
      MERGE dbo.tblHistory AS Target
      USING (SELECT * FROM inserted) AS Source 
         ON (Target.ID = Source.ID AND Target.EndDate = @maxDate)
  
      WHEN MATCHED AND Target.StartDate &gt; @yesterday THEN
          DELETE
  
      WHEN MATCHED THEN
          UPDATE 
              SET Target.EndDate = @yesterday
      ;
  
      --Insert updated row into History here
  END
  </code></pre>
  
  <p>This seems to be the most human-readable and logical way to do it. However I'm not sure if it's the most effective.</p>