title: SQL Server trigger for changing value of column of inserted row according to delta from other table after insert?
body: <p>I have 2 tables in my SQL Server database, for example <code>[Camera]</code> and <code>[CameraData]</code>. How to write a trigger which will change value in <code>[CameraData]</code> after row is inserted into <code>[CameraData]</code> due to delta in <code>[Camera]</code>.</p>
  
  <p>For example we have 2 cameras in <code>[Camera]</code>:</p>
  
  <ul>
  <li>Camera 1 with {id} = 1 and {delta} = null</li>
  <li>Camera 2 with {id} = 2 and {delta} = 3</li>
  </ul>
  
  <p>So when we have automated insert into table <code>[CameraData]</code>, f.e. :</p>
  
  <pre><code>Id_camera = 2, angle = 30, Changed = null 
  </code></pre>
  
  <p>In that case we need to check either we have delta in [Camera] on camera 2 and if that's true we need to modify insert to:</p>
  
  <pre><code>Id_camera = 2, angle = 33 (angle + Camera.Delta), Changed = True
  </code></pre>
  
  <p><strong>Update 1</strong></p>
  
  <p>According to comment [3] is the column in table <code>[CameraData]</code> where angle is placed</p>
  
  <pre><code>CREATE TRIGGER Delta_Angle
  ON CameraData
  AFTER INSERT
  AS
  BEGIN
      SET NOCOUNT ON;
  
      UPDATE CameraData
      SET DeltaFlag = 1, [3] = inserted.[3] + i.DeltaAngle
      FROM CameraData h
      INNER JOIN Camera i ON h.ID_Camera = i.ID_Camera
      WHERE i.DeltaAngle != ''
  END
  </code></pre>