title: How to copy an inserted,updated,deleted row in a SQL Server trigger(s)
body: <p>If a user changes table <code>HelloWorlds</code>, then I want 'action they did', time they did it, and a copy of the original row insert into <code>HelloWorldsHistory</code>.</p>
  
  <p>I would prefer to avoid a separate triggers for insert, update, and delete actions due to the column lengths.</p>
  
  <p>I've tried this:</p>
  
  <pre><code>create trigger [HelloWorlds_After_IUD] on [HelloWorlds]
  FOR insert, update, delete
  as
  if @@rowcount = 0 
  return
  if exists (select 1 from inserted) and not exists (select 1 from deleted)
  begin
  insert into HelloWorldHistory (hwh_action, ..long column list..)
  select 'INSERT', helloWorld.id, helloWorld.text ... and more from inserted
  end
  else
      if exists (select 1 from inserted) and exists (select 1 from deleted)
      begin
  insert into HelloWorldHistory (hwh_action, ..long column list..)
  select 'UPDATE', helloWorld.id, helloWorld.text ... and more from deleted 
      end
      else
      begin
  insert into HelloWorldHistory (hwh_action, ..long column list..)
  select 'DELETE', helloWorld.id, helloWorld.text ... and more from deleted
      end
  end
  </code></pre>
  
  <p>I've never seen an insert appear, but I've seen updates.  I'm going to try 3 separate triggers, though maintaining the column lists will not be fun.</p>