title: Create trigger to insert a column value into other column of same table SQL Server 2005
body: <p>How to create trigger to insert a value in a column same as value of another column of same table of a newly inserted row in the table.</p>
  
  <p>Suppose I have table like below</p>
  
  <pre><code>ColumnA | ColumnB
  </code></pre>
  
  <p>I want columnB value to be inserted into ColumnA as soon as row gets inserted into table or columnB value gets updated . But it should not be vice versa ie insert columnA value into columnB</p>
  
  <p>.Below code handles INSERT only , please help me how to handle both <strong>Insertion and Updation</strong> of table ie when columnB value gets inserted or updated.</p>
  
  <pre><code>CREATE TRIGGER inserupdate
     ON  triggertestTable
     AFTER INSERT,UPDATE
  AS 
  BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
  
      -- Insert statements for trigger here
      declare @value int
  
      select @value=columnB from inserted
  
      update triggertestTable
      set columnA=@value
  
  END
  GO
  </code></pre>
  
  <p>This works fine if values inserted like below</p>
  
  <pre><code>insert into triggertestTable(columnB) values('xyz')
  </code></pre>
  
  <p>ColumnB value gets inserted into columnA</p>
  
  <pre><code>ColumnA | ColumnB
    xyz   |  xyz 
  </code></pre>
  
  <p>But null value gets updated in both if some other application inserts value in columnA</p>
  
  <pre><code> insert into triggertestTable(columnA) values('pqr')
  </code></pre>
  
  <p>Now records are</p>
  
  <pre><code>ColumnA | ColumnB
    xyz   |  xyz 
    NULL  |  NULL
  </code></pre>
  
  <p>The correct recordset should be like below</p>
  
  <pre><code>  ColumnA   | ColumnB
        xyz   |  xyz 
        pqr   |  NULL
  </code></pre>
  
  <p>How to solve this problem.</p>