title: My Trigger wont work
body: <p>I have been working on a trigger for my SQL database.</p>
  
  <p><strong>The tables:</strong></p>
  
  <p>TABLE1: ID(int, auto increment), value1(varchar), value2(varchar), count(int)</p>
  
  <p>TABLE1_History: ID(Separate from TABLE1.ID, int, auto increment), same columns as TABLE1 but with an added TimeStamp(smalldatetime)</p>
  
  <p><strong>The code:</strong></p>
  
  <pre><code>    SELECT @@ROWCOUNT FROM TABLE1_History, INSERTED
      WHERE TABLE1_History.value1 LIKE INSERTED.value1
      AND TABLE1_History.value2 LIKE INSERTED.value2
      AND DATEDIFF(HOUR,TABLE1_History."TimeStamp", GETDATE()) &gt; 24;
  
      IF(@@ROWCOUNT = 0)
      BEGIN
          INSERT INTO TABLE1_History(value1, value2, count, "TimeStamp")
          SELECT value1, value2, count, GETDATE() FROM INSERTED
      END
      ELSE
      BEGIN
          UPDATE TABLE1_History
          SET "TimeStamp" = GETDATE(), count = count+(SELECT count FROM INSERTED)
          WHERE TABLE1_History.value1 LIKE (SELECT value1 FROM INSERTED)
          AND TABLE1_History.value2 LIKE (SELECT value2 FROM INSERTED)
          AND DATEDIFF(HOUR,TABLE1_History."TimeStamp", GETDATE()) &lt; 24;
      END
  END
  </code></pre>
  
  <p><strong>What's supposed to happen:</strong></p>
  
  <p>When I Add a new row to TABLE1 I want TABLE1_History to update.</p>
  
  <p>TABLE1_History <em>ONLY</em> adds new rows if a row already exists with the same value1&amp;value2 as the new row <em>AND</em> if it's been more than 24hours since that row was added.</p>
  
  <p>Otherwise, the counter increases.</p>
  
  <p><strong>What happens:</strong></p>
  
  <p>As of now: TABLE1_History gets updated every time. Doesn't matter if the values match or not. </p>
  
  <p>It seemed to work before I decided to let TABLE1_History.ID be separate from TABLE1.ID.</p>