Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table-level diff and sync procedure for T-SQL

I'm interested in T-SQL source code for synchronizing a table (or perhaps a subset of it) with data from another similar table. The two tables could contain any variables, for example I could have

 base table    source table 
 ==========    ============
 id     val    id       val
 ----------    ------------
 0        1    0          3
 1        2    1          2
 2        3    3          4

or

 base table             source table 
 ===================    ==================
 key    val1    val2    key   val1    val2
 -------------------    ------------------
 A         1       0    A        1       1  
 B         2       1    C        2       2
 C         3       3    E        4       0

or any two tables containing similar columns with similar names. I'd like to be able to

  • check that the two tables have matching columns: the source table has exactly the same columns as the base table and the datatypes match
  • make a diff from the base table to the source table
  • do the necessary updates, deletes and inserts to change the data in the base table to correspond the source table
  • optionally limit the diff to a subset of the base table,

preferrably with a stored procedure. Has anyone written a stored proc for this or could you point to a source?

like image 774
Ville Koskinen Avatar asked Mar 10 '10 09:03

Ville Koskinen


People also ask

How can I sync two tables in different location in SQL Server?

If the answers are both yes, then you can use sql server's tablediff.exe command line utility. It can do table-by-table, one-off compare between two tables and generate the sql automatically for you to sync the dest to the source.

How do you sync two tables in different databases?

Specifically, you can create a batch sync node, specify tables in multiple databases as the source tables, and then specify the destination table. After that, you can run the batch sync node to synchronize the data from the source tables to the destination table.

How can I compare two table data types in SQL Server?

Compare SQL Server Data in Tables Using the Tablediff Tool This can be found in "C:\Program Files\Microsoft SQL Server\110\COM\" folder. This command line tool is used to compare tables. It also generates a script with the INSERT, UPDATE and DELETE statements to synchronize the tables.


2 Answers

SQL Server 2008 features the new merge statement. It's very flexible, if a bit complex to write out.

As an example, the following query would synchronize the @base and @source tables. It's limited to a subset of @base where id <> 2:

MERGE @base as tgt
USING @source as src
ON tgt.id = src.id and tgt.val = src.val
WHEN NOT MATCHED BY TARGET
    THEN INSERT (id, val) values (src.id, src.val)
WHEN NOT MATCHED BY SOURCE AND tgt.id <> 2
    THEN DELETE
like image 88
Andomar Avatar answered Oct 02 '22 15:10

Andomar


Interesting question.

you could start from EXCEPT - INTERSECT

http://msdn.microsoft.com/en-us/library/ms188055.aspx

Here is readymade solution, may help you

http://www.sqlservercentral.com/scripts/Miscellaneous/30596/

like image 24
Saar Avatar answered Oct 02 '22 14:10

Saar