title: Stored procedure for generic MERGE
body: <p>I have a set of 10 tables in a database (<code>DB1</code>). And there are 10 tables in another database (<code>DB2</code>) with exact same schema on the same SQL Server 2008 R2 database server machine.</p>
  
  <p>The 10 tables in <code>DB1</code> are frequently updated with data.</p>
  
  <p>I intend to write a stored procedure that would run once every day for synchronizing the 10 tables in <code>DB1</code> with <code>DB2</code>. The stored procedure would make use of the <code>MERGE</code> statement.</p>
  
  <p>Now, my aim is to make this as generic and parametrized as possible. That is, accommodate for more tables down the line... and accommodate different source and target DB names. Definitely no hard coding is intended.</p>
  
  <p>This is my algorithm so far:  </p>
  
  <ol>
  <li>Have the database names as parameters  </li>
  <li>Have the first query within the stored procedure... result in giving the names of the 10 tables from a lookup table (this can be 10, 20 or whatever)  </li>
  <li>Have a generic <code>MERGE</code> statement that does the sync for each of the above set of tables (based on primary key?)  </li>
  </ol>
  
  <p>This is where I need more inputs on.</p>
  
  <p>What is the best way to achieve this stored procedure? SQL syntax would be helpful.</p>