Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6616829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:37:49+00:00 2026-05-25T20:37:49+00:00

I have two databases with identical table layouts. There are a dozen or so

  • 0

I have two databases with identical table layouts. There are a dozen or so tables of interest. They are a number of FK between them.

I have been asked to write a stored procedure to copy data from database A to database B based on the PK of the parent table at the top of the hierarchy. I may receive just one value, or a list of values. I’m supposed to select all records from database A that match the value(s) and insert/update them into database B. This includes all the records in the child tables too.

My questions is whats the best(most efficent/ best practice) way to do this?

Should I write a dozen select from… insert into… statements?

Should I join the tables together an try to insert into all the tables at the same time?

Thanks!

Additional info:
The record should be inserted if it is not already there. (based on the PK of the respective table). Otherwise it should be updated.

Obviously I need to traverse down to all child tables, so There would only be one record to copy in the parent table, but the child table might have 10, and the child’s child table might have 500. I would of course need to update the record if it already existed, insert if it does not for the child tables too…

UPDATE:
I think it would make the solution simpler if I just deleted all records related to the top level key, and then insert all the new records rather than trying to do updates.

So I guess the questions is it best to just do a dozen:

delete from ... where ... in ...

select from ... where ... in ...
insert into...

or is it better to do some kinda of fancy joins to do all the inserts in one sql statement?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T20:37:50+00:00Added an answer on May 25, 2026 at 8:37 pm

    I would do this by disabling all the foreign key constraints, then doing a set of MERGE statements to deal with the updates and inserts, then enable all the constraints.

    Think about logging. How much redo do you want to generate?

    You might find that it’s quicker and better to truncate all the target tables and then do inserts of everything with nolog. Could be simpler than the merges.

    One major main alternative would be to drop all the target tables and use export and import. Might be a lot faster.

    A second alternative would be to use materialized views, particularly if you don’t need to do updates on the target tables. That way, Oracle does all the heavy lifting for you. You can force integrity by choosing refresh groups carefully.

    There are several ways to deal with this business problem. A PL/SQL program may not be the best.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two identical tables in two different databases with the same data but
I have two tables in two separate sqlite3 databases. The datatypes are identical, but
Let's say I have two databases with identical tables, but one database's tables contains
I have two MySQL databases with identical table structure, each populated with several thousand
I have two databases at two different places, both databases are same with table
I have two MySQL databases: one for testing, and one for production. They are
I have two tables, both named Language in two different schemas, lets call them
Suppose I have two distinct databases with identical schemas but different data. I want
Here's the scenario. Two identical databases: One Live database, one Archive database, they're suppose
Given two MS SQL databases which are known to have identical schemas, how should

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.