Avatar of Hari Shankar
Hari Shankar
Flag for India asked on

How do i insert and update the data if already exisits in database ?

Hi Experts,

We have c# datatable and need to import to the SQL Table . And am aware of SQLBulkCopy Method.but it doesnot update the data if already presents . can i have any alternative which can update if present as well as and inserts if not presents

Thanks in Advance
DatabasesC#SQL

Avatar of undefined
Last Comment
Pawan Kumar

8/22/2022 - Mon
Pawan Kumar

You can use below-

If exists ( select top 1 1 from yourtable where col1 = @val1 and col2 = @col2.. )
Begin
     Update statement..
End
Else
Begin
    Insert statement..
End
Ryan Chong

And am aware of SQLBulkCopy Method.but it doesnot update the data if already presents
you still can perform a Bulk copy , but try to populate the data into a temporary (staging) table first, and then do a mass updates for the fields based on unique key verification. And finally do the data insertion for new entries.
awking00

What dbms are you using?
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
SOLUTION
Tomas Helgi Johannsson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
Pawan Kumar

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.