title: TSql Trigger To Delete Rows from Table1 Upon Insert to Table2
body: <p>I've read and tried other posts/answers but am having a hard time getting this to work.  I feel like it should be pretty simple for an experienced SQL programmer, but for me I'm ready to ask for help.</p>
  
  <p>When I insert a record into the <code>HistoricalWorkorders</code> table, I simply want to delete any records with the same <code>workorder #</code> in the <code>ActiveWorkorders</code> table.</p>
  
  <p>I'm trying to accomplish this using a trigger on insert.</p>
  
  <p>I started simple by using the following code to insert into <code>ActiveWorkorders</code> every time a record is inserted into <code>HistoricalWorkorders</code>.  That code works.</p>
  
  <hr>
  
  <blockquote>
    <p><strong>How</strong> do I modify this query such that upon insert to the <code>HistoricalWorkorders</code> table, any records with the same <code>workorder #</code> within the <code>ActiveWorkorders</code> table get deleted?</p>
  </blockquote>
  
  <hr>
  
  <p>On a seperate note, everyone says you can reference the inserted record simply by referencing <code>'inserted'</code>, but it doesn't work for me?  For example, shouldn't <code>inserted.workorder</code> return the <code>workorder</code> value being inserted to the table?</p>
  
  <pre><code>ALTER trigger [dbo].[WOArchive] on [dbo].[HistoricalWorkOrders]
  after update,insert
  as
  begin
  insert into ActiveWorkorders
  (Workorder,Grindcount,Status)
  select i.workorder, i.grindcount, i.status
  from HistoricalWorkorders t
  inner join inserted i on t.workorder=i.workorder
  end
  </code></pre>