Results 1 to 5 of 5
  1. #1
    Join Date
    Nov 2002

    Unanswered: Combining two tables from different databases

    I have two databases that each contain the same tables, but different data in the tables. For example, each data contains a table with the same name, arcus, that holds data on our customers. Although the data is different in each table, there is some overlap, particularly in the area of customer number since that is assigned automatically when a customer is entered and serves as the primary key for that table.

    To consolidate, I need to merge the two databases. How can I import the data from one table in second database into a table in the first database and append a number to the customer number so that all data will be brought across.

    To better illustrate:

    database one has an arcus file with a field cusno and contains cusno 1-50
    database two has an arcus file with a field cusno and contains cusno 27-58

    The overlapping cusno's are not the same customer.

    How can I get all the cusno from the arcus file in database two to the arcus file in database one?

    Is this even possible?

  2. #2
    Join Date
    Jul 2004
    not without creating all new PK values.

    if you are OK with a brand new value for the PK, which is sounds like you are, i'd create a staging table with an identity on the front of it and insert all the data from both, and use the new identity as your new cusno, replacing table in database1 (after renaming it with a '_BACKMEUPFOO' suffix)

    in any scenario you face one big hurdle:
    you will be breaking all the relationships FK'd to cusno in database #1.
    all those related tables will need updating too...and the app that creates this data may not like it non-too-much, you changing its' PK and all.

  3. #3
    Join Date
    Jul 2003
    San Antonio, TX
    You can use either DTS or BCP...OUT. If you use DTS you can easily skip the IDENTITY field values and append from one database table to the other. If you choose BCP you'll have to create a format file during OUT operation, edit it with a text editor to specify that you are going to skip the IDENTITY field, and then BCP...IN/BULK INSERT specifying that modified format file.
    "The data in a record depends on the Key to the record, the Whole Key, and
    nothing but the Key, so help me Codd."

  4. #4
    Join Date
    Jun 2003
    Provided Answers: 1
    ...which is why I like to use GUIDs as surrogate keys rather than incrementing identities.
    If it's not practically useful, then it's practically useless.

    blindman "sqlblindman"

  5. #5
    Join Date
    Jul 2004
    sounds like your need is to retain the original cusno's in some derivable fashion, and to do that you're going to need to create a surrogate or change the PK in the target database entirely - maybe compound it by adding a 'source system' character column to it. or just tack an 'a' on the end off all the original cusnos from the 1st server and a 'b' to all the second.

    the relationship breaking is still gonna hurt you, without updating all the rest of the tables FK'd to cusno in your target - no matter how you pump the data or change the cusno.

    DTS would be my ETL tool of choice - if i had to pick btw BCP and DTS, for this job.

Posting Permissions

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