Multiple tables inserts

  • Hello,

    I have next task:

    From one database, 3 tables (each 3 fields are chosen) to insert in another database's fields from 4 tables.

    First source database is simple, will create a view.

    Any good documentation on how not to break relationships in destination database.

    In addition, how to just update the destination after first upload of all.

    Thanks,

    Brano

  • branovuk (6/5/2013)


    Hello,

    I have next task:

    From one database, 3 tables (each 3 fields are chosen) to insert in another database's fields from 4 tables.

    First source database is simple, will create a view.

    Any good documentation on how not to break relationships in destination database.

    In addition, how to just update the destination after first upload of all.

    Thanks,

    Brano

    Probably the easiest way to do this is to add some additional columns to your destination tables to hold the keys from the data as it exists in the current database. Then you can update the rows after they have been inserted by leveraging the "old" key values. Then drop the additional columns.

    For example let's say you have Department and Employee tables. You would add a OldDepartmentID to the source Department table and an OldEmployeeID on the source Employee table.

    Then you simply insert all the rows from both those tables.

    Then you do something like this to update the foreign key to department on the new rows.

    Update e set DepartmentID = d.DepartmentID

    from Employee e

    join Department d on d.OldDepartmentID = e.OldDepartmentID

    That make sense?

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Thanks Sean,

    I will work on this one :-)!

    Regards,

    Brano

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply