Upsert

 

Upsert data in Database Table using Update and Insert Operation

In this tutorial we will learn how to upsert  data in database table using Insert and Update operation because there is no out of box functionality available to upsert the data  in database another alternative is to use DB Merge operation to merge the data in database.

Create a project in anypoint studio

Sample Data to be inserted/updated in database table

[
  {
    "CUSTOMER_ID": 1,
    "CUSTOMER_NAME": "TEST1",
    "ADDRESS1": "Flat 101",
    "ADDRESS2": "Viman Nagar",
    "POSTALCODE": "201018",
    "CITY": "PUNE"
  },
  {
    "CUSTOMER_ID": 2,
    "CUSTOMER_NAME": "TEST2",
    "ADDRESS1": "Flat 102",
    "ADDRESS2": "Viman Nagar 1",
    "POSTALCODE": "201019",
    "CITY": "PUNE"
  },
  {
    "CUSTOMER_ID": 3,
    "CUSTOMER_NAME": "TEST3",
    "ADDRESS1": "Flat 103",
    "ADDRESS2": "Viman Nagar",
    "POSTALCODE": "201018",
    "CITY": "PUNE"
  },
  {
    "CUSTOMER_ID": 4,
    "CUSTOMER_NAME": "TEST4",
    "ADDRESS1": "Flat 104",
    "ADDRESS2": "Viman Nagar 4",
    "POSTALCODE": "201019",
    "CITY": "PUNE"
  }
]

 

Drag and drop the Transform message and prepare the Input for testing

Upsert

 

Drag and drop select operation and wrap inside foreach loop and configure the DB connector

Upsert

 

Configure the select,insert and update operation.

Using for each loop we will process all records one by one in database, first we will select the count for the each single record in database if the record exist we will update the record else we will insert the record in DB. We will use choice router to check data is present in db or not using record count variable

Select configuration

Upsert

 

Update Operation:

 

Insert Operation:

 

Deploy the application and test

download sample project

  
Thank you for taking out time to read the above post. Hope you found it useful. In case of any questions, feel free to comment below. Also, if you are keen on knowing about a specific topic, happy to explore your recommendations as well.
 
For any latest updates or posts on our website, you can follow us on LinkedIn. Look forward to connecting with you there.


Share this:
Subscribe
Notify of
guest
8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Neena
Neena
4 years ago

HI Team, I am using Upsert logic as shown above in my logic. But what will be the performance when it has say 10 000 records to upsert. Is there a way to make it more efficient? I tested and it takes 20 minutes for the above logic. I am using Oracle DB for this.

Neena
Neena
4 years ago
Reply to  admin

HI Team, Is batch processing a good solution here?

Mina
Mina
3 years ago

Hi what u have written in choice when condition? it is not visible completely in pic

gani
gani
3 years ago

HI
vars.count.count[0] == 1
error: null cant refer to a number .

Can you help me on above issue.

gani
gani
3 years ago

i am doing the same but my table is not getting updated its skipping from update part and going to default. vars.recotdcount.recordcount[0] returns null and compares with == number 1.

flow.png