0

I have two tables. I need to fetch data from one table and insert it into the other table, provided the data (say ID) does not exist in the second table. If the data exists, I need to update the values in the table for that id.

How can we achieve that?

I first collect all data from the first table in an arraylist, and iterate through the list. If the element presents in the second table, I call the update query. If not, I call the insert query.

Any other way to achieve this? I think it takes so much time, even though its straightforward.

PS: I dont have my query with me now

1
  • If both tables are same, you can truncate all the rows in 2nd table and run a single insert query from 1st table. Commented Jan 17, 2013 at 17:38

1 Answer 1

1

Look at the MERGE statement. This is what you are looking for: MERGE WHEN EXISTS THEN UPDATE NOT EXISTS THEN INSERT (This is not the syntax)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.