title: Sql Trigger to add new row when table update
body: <p>I am new to sql triggers and learning as developing triggers for asp.net application.i am having case where I need to first save table and then edit the same table - this edit create new row in different table but the the problem is every time I edit the table it create new row in different table I want to create row in different table for only first edit.</p>
  
  <p>Dividing my problem for readability. </p>
  
  <p>I have two tables:
  Table A and table B</p>
  
  <p>I have written trigger on table A that add row in table B.</p>
  
  <p>Problem:
  Every time I edit row in table A a new row get added to table B. (So every edit create new row)</p>
  
  <p>Required result:
  I want my trigger to add ONLY one row in table B for the first edit in table A but not for subsequent edits.</p>
  
  <p>I am using update triggers.
  Any example with code would be great 
  Thanks you much in advance .</p>
  
  <pre><code>Create TRIGGER [dbo].[triggerName] ON [dbo].[databaseName]
  For Update
  As
  Begin
  DECLARE @i int
  DECLARE @d int
  DECLARE @action char(6)
  DECLARE @Car VARCHAR(20)
  
  IF (@@ROWCOUNT = 0) RETURN
  
  SELECT @i = Count(*) From Inserted
  SELECT @d = Count(*) From Deleted
  
  SELECT @action = CASE 
  WHEN (@i &lt;&gt; 0) and (@d &lt;&gt; 0) THEN 'UPDATE'
  WHEN (@i = 0) and (@d &lt;&gt; 0) THEN 'DELETE'
  WHEN (@i &lt;&gt; 0) and (@d = 0) THEN 'INSERT'
  End
  
  
  SELECT @Car = A From inserted
  
  
  IF @action = 'UPDATE' AND @Car in ('BMW')
  
     Begin
      INSERT INTO Tableb (c,d,f)
      Select c,d,f from inserted
     End
  </code></pre>