title: Remove duplicates before insert sql
body: <p>I have temporary table #tempRD and I am trying to insert the resultset into a table as follows:</p>
  
  <pre><code>insert into Routing (RoutingKeyID, LocationID, Data, ServiceID, CountryID)
     select 
         rk.ID, rd.LocationID, rd.Data, rd.service, rd.CountryID
     from 
         #tempRD rd
     inner join 
         RoutingKey rk on rk.serviceID = @ID and rk.Name=rd.[Key]
  </code></pre>
  
  <p>Now when this happens I get duplicate key errors </p>
  
  <blockquote>
    <p>Cannot insert duplicate key row in object 'dbo.Routing' with unique index 'UIX_Routing_RoutingKeyID_CountryID'. The duplicate key value is (51, 433)</p>
  </blockquote>
  
  <p>How can I check if a row by <code>routingkeyid</code> and <code>countryid</code> already exists before I do an insert?</p>
  
  <p>I have used a cursor to do this but it takes a long long time.</p>