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
Drag and drop select operation and wrap inside foreach loop and configure the DB connector
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
Update Operation:
Insert Operation:
Deploy the application and test
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.
Hi Neena,
We can update the for loop with for loop parallel to improve the performance otherwise we can write a db procedure which can perform the upsert logic.
Thanks,
Mulesy Team
HI Team, Is batch processing a good solution here?
Yes, We can use Batch Job here. Use batch aggregator as well to insert multiple records in one go.
Hi what u have written in choice when condition? it is not visible completely in pic
Please download the sample project to see the code.
HI
vars.count.count[0] == 1
error: null cant refer to a number .
Can you help me on above issue.
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.