Reputation: 391
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
Upvotes: 0
Views: 67
Reputation: 627
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)
Upvotes: 1