title: How to compare rows in source and destination tables dynamically in SQL Server
body: <p>We receive a data feed from our customers and we get roughly the same schema each time, though it can change on the customer end as they are using a 3rd party application.  When we receive the data files we import the data into a staging database with a table for each data file (students, attendance, etc).  We then want to compare that data to the data that we already have existing in the database for that customer and see what data has changed (either the column has changed or the whole row was possibly deleted) from the previous run.  We then want to write the updated values or deleted rows to an audit table so we can then go back to see what data changed from the previous data import.  We don't want to update the data itself, we only want to record what's different between the two datasets. We will then delete all the data from the customer database and import the data exactly as is from the new data files without changing it(this directive has been handed down and cannot change).  The big problem is that I need to do this dynamically since I don't know exactly what schema I'm going to be getting from our customers since they can make customization to their tables.  I need to be able to dynamically determine what tables there are in the destination, and their structure, and then look at the source and compare the values to see what has changed in the data.</p>
  
  <p>Additional info:
  There are no ID columns on source, though there are several columns that can be used as a surrogate key that would make up a distinct row.</p>
  
  <p>I'd like to be able to do this generically for each table without having to hard-code values in, though I might have to do that for the surrogate keys for each table in a separate reference table.</p>
  
  <p>I can use either SSIS, SPs, triggers, etc., whichever would make more sense.  I've looked at all, including tablediff, and none seem to have everything I need or the logic starts to get extremely complex once I get into them.</p>
  
  <p>Of course any specific examples anyone has of something like this they have already done would be greatly appreciated.</p>
  
  <p>Let me know if there's any other information that would be helpful.</p>
  
  <p>Thanks</p>