Results 1 to 15 of 15

Thread: Data Transfer between SQL Server

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Location
    Medan, Indonesia
    Posts
    7

    Data Transfer between SQL Server

    i want to create an application that can do syncronization (data transfer) between 2 SQL server (the dbstructure in each server are identical, lets say db1 and db2 for the database in these server). is there any method to compare the data stored in table in db1 and db2 (table1 and table2), and if different found in table2, then copy from table1 to table2.
    i know that doing looping for these table and comparing each of the records can do this job, but it is very troublesome and ofcourse take a very long time.
    and then i found out about dataset / datatable merge method, but i cannot actually get this method to work. is there anyone can help me with this datatable.merge methodology or any other better method to use.

    i have already tried something like this:
    VB Code:
    1. Private Function CopyData() As Boolean
    2.         Dim strSQL As String = ""
    3.         Dim sqladpData As System.Data.SqlClient.SqlDataAdapter
    4.         Dim dtSrc As System.Data.DataTable 'source data table
    5.         Dim dtDest As System.Data.DataTable 'destination data table
    6.         Dim dtChanges As System.Data.DataTable 'changes data table
    7.         Dim drChange As System.Data.DataRow
    8.         Dim dcPrimary(0) As System.Data.DataColumn
    9.         Dim sqlcbrUpdate As SqlClient.SqlCommandBuilder
    10.  
    11.         '---- Company ----
    12.         strSQL = "Select * From Company order by Code"
    13.         'source data
    14.         sqladpData = New System.Data.SqlClient.SqlDataAdapter(strSQL, g_objSrcConn)
    15.         dtSrc = New System.Data.DataTable
    16.         sqladpData.Fill(dtSrc)
    17.         'destination data
    18.         sqladpData = New System.Data.SqlClient.SqlDataAdapter(strSQL, g_objDestConn)
    19.         dtDest = New System.Data.DataTable
    20.         sqladpData.Fill(dtDest)
    21.         'setup primary keys
    22.         ReDim dcPrimary(0)
    23.         dcPrimary(0) = dtSrc.Columns(0)
    24.         dtSrc.PrimaryKey = dcPrimary
    25.         dcPrimary(0) = dtDest.Columns(0)
    26.         dtDest.PrimaryKey = dcPrimary
    27.  
    28.         'merge source & destination to get changes
    29.         dtDest.Merge(dtSrc, True, MissingSchemaAction.Ignore)
    30.     End Function
    but i cannot update the merge result (newly added row from source) to the destination table in destination server

  2. #2
    Lively Member
    Join Date
    Nov 2006
    Posts
    116

    Re: Data Transfer between SQL Server

    Instead of writting code to do the synchronization, may be can explore on the data replication in the SQL server to get it done automatically.

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Location
    Medan, Indonesia
    Posts
    7

    Re: Data Transfer between SQL Server

    i need user to do this synchronization on their own because they will do this daily basis on some master table and also transactions table. users are the one who capable to know when is the right time to do the synchronization, the sync process might be done once a day, twice a week or even several time a day based on their transactions volume that needed to upload to remote location. so basically its not local (main) server push to remote (branches) servers, but remote servers pull from local server

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Data Transfer between SQL Server

    It is extremely fast (and simple code as well) to produce a TEXT file that is tab delimited export from a table.

    It is equally simple to write code to compare these two text files and output INSERT, UPDATE or DELETE statements based on the differences between the two text files.

    And you can obviously take these INSERT/UPDATE/DELETE scripts and run them against your DB.

    How big are these tables? How many rows? Are the two servers local to each other?

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5
    PowerPoster
    Join Date
    Jul 2002
    Location
    Dublin, Ireland
    Posts
    2,148

    Re: Data Transfer between SQL Server

    You can do this with the BCP program (Bulk copy) which is a command line application that comes with SQL server.

    Type
    Code:
    bcp /?
    at teh command line to get the params needed

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Location
    Medan, Indonesia
    Posts
    7

    Re: Data Transfer between SQL Server

    Quote Originally Posted by szlamany
    How big are these tables? How many rows? Are the two servers local to each other?
    several master tables (min 50 recs, max 1000 recs), and 6 daily transaction tables (1000++ recs each)

    @Merrion: i will have a look at this "bcp" things first, thx

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Data Transfer between SQL Server

    Before you go the route of BCP.... the SQLClient namespace in .NET has a built-in method BulkInsert ... I recently used it to import DBF files just make a connection to the source data, fill a dataset/table... then make another connection to the SQL Server, and use the BulkInsert call, passing in the DataTable from the source. Slurrrp! In it goes. Fast, easy and simple.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Location
    Medan, Indonesia
    Posts
    7

    Re: Data Transfer between SQL Server

    i cannot find out how bcp can do comparison between the source data & the destination before inserting the data. what i want here is not simply copy all data from source to destination, but to only copy the data that is added (and if possible, modified).

    btw, @szlamany : the source server is local while the destination is remote location

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Data Transfer between SQL Server

    I find that the method I suggested is most appropriate.

    Having a "text file" of each of the servers data be delivered to a central location where a simple VB program runs through them comparing row to row.

    New rows in one file cause INSERT statements to be written to a TEXT FILE. If they are missing (and it's what you want) then a DELETE statement is written to the TEXT FILE. Otherwise if the two primary keys of the row match a column-by-column compare is done (of the text file) and an UPDATE statement is written to make the databases match.

    All these are written to a TEXT FILE that is then executed on the "destination server" using ISQL/OSQL (command line SQL query executor).

    I find that this method of getting tables to match when the servers are remote is best. Being able to see the "TEXT FILE" of what updates will occur is helpful. Archiving these text files makes lots of sense.

    This method also automates nicely - couple of .BAT files that can be run from a scheduled job or even the server agent.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  10. #10

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Location
    Medan, Indonesia
    Posts
    7

    Re: Data Transfer between SQL Server

    using 2 text file, how to compare if row in text1 exist in text2? should i do two times looping for this (loop for text1 lines and text2 lines)? or is there any better and faster way to do the checking?

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Data Transfer between SQL Server

    Both text files need to be sorted in the exact same primary key order - this is paramount.

    Otherwise - the logic is simple.

    Pseudo-code:

    1) Get a record from text file 1
    2) Get a record from text file 2
    3) If the primary key's in both records exist then
    ...compare columns
    ......Set a flag to get a record from both text files 1 and text file 2
    4) If the primary key of text file 1 is less then text file 2
    ...then text file 1 row does not exist in text file 2
    ......Deal with this fact - set a flag to get a row from only text file 1
    5) If the primary key of text file 2 is less then text file 1
    ...then text file 2 row does not exist in text file 1
    ......Deal with this fact - set a flag to get a row from only text file 2

    dealing with the fact is either an INSERT or DELETE statement - based on which table you feel is the production data...

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Location
    Medan, Indonesia
    Posts
    7

    Re: Data Transfer between SQL Server

    for pseudo-code 1) and 2), how can i get the record from the text file? read line by line from text1, and for each line I do another line by line reading from text2 to find if there is any matching primary key?
    wont this become very slow if i got alot of records?

  13. #13
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Data Transfer between SQL Server

    This is a proven technique - used by us on tables with millions of rows.

    The tables must be sorted in primary key order.

    If all the rows happen to exist the same between both tables then you read a row from text file 1 and text file 2 - they match - you continue.

    There is nothing faster than doing this.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  14. #14

    Thread Starter
    New Member
    Join Date
    Jan 2007
    Location
    Medan, Indonesia
    Posts
    7

    Re: Data Transfer between SQL Server

    both textfiles having same rows cannot guarantee that this two textfiles is the same right? it is posible that text1 (source) have added a row and deleted another one (which makes the number of rows the same).
    can you give a little logic of how to compare this two text file and determine which one i need to insert/update/delete from destination database
    thx b4

    text1
    -A0001 Alpha
    -A0002 Alexander
    -B0004 Bingo
    -C0002 Charles
    -C0004 Caroline

    text2
    -A0001 Alpha
    -A0002 Alexis
    -B0004 Bingo
    -C0003 Charlie
    Last edited by alexyie; Jan 23rd, 2007 at 07:17 AM.

  15. #15
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Data Transfer between SQL Server

    I took your two files and created .TXT files on my C: drive...

    This does it:

    VB Code:
    1. Private Sub Form_Load()
    2.  
    3. Open "c:\file1.txt" For Input As #1
    4.  
    5. Open "c:\file2.txt" For Input As #2
    6.  
    7. Dim booGet1 As Boolean, booGet2 As Boolean
    8.  
    9. Dim str1 As String, str2 As String
    10.  
    11. booGet1 = True
    12. booGet2 = True
    13.  
    14. While booGet1 Or booGet2
    15.  
    16.     If Not EOF(1) And booGet1 Then Input #1, str1
    17.    
    18.     If Not EOF(2) And booGet2 Then Input #2, str2
    19.    
    20.     Debug.Print str1; " * "; str2
    21.    
    22.     booGet1 = False
    23.     booGet2 = False
    24.    
    25.     If Left(str1, 6) = Left(str2, 6) Then
    26.         Debug.Print "Same keys - we should compare rows and make UPDATE statement if needed"
    27.         booGet1 = True
    28.         booGet2 = True
    29.     Else
    30.         If Left(str1, 6) < Left(str2, 6) Then
    31.             Debug.Print "Exists in file1 - need to add"
    32.             booGet1 = True
    33.         Else
    34.             If Left(str1, 6) > Left(str2, 6) Then
    35.                 Debug.Print "Exists in file2 - need to delete"
    36.                 booGet2 = True
    37.             End If
    38.         End If
    39.     End If
    40.  
    41.     If EOF(1) And EOF(2) Then
    42.         booGet1 = False
    43.         booGet2 = False
    44.     End If
    45.  
    46. Wend
    47.  
    48. End Sub

    displays this in the immediate window - note I assumed that one file was primary - meaning if it existed in that file we would add, if it did not exist in that file we would delete. That logic is up to you - but this is the very simple logic of comparing two text files for PK's and keeping each one aligned to the other.

    Code:
    -A0001 Alpha * -A0001 Alpha
    Same keys - we should compare rows and make UPDATE statement if needed
    -A0002 Alexander * -A0002 Alexis
    Same keys - we should compare rows and make UPDATE statement if needed
    -B0004 Bingo * -B0004 Bingo
    Same keys - we should compare rows and make UPDATE statement if needed
    -C0002 Charles * -C0003 Charlie
    Exists in file1 - need to add
    -C0004 Caroline * -C0003 Charlie
    Exists in file2 - need to delete

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width