Additional SQL Server features and topics not covered by specific categories
Hi @techresearch7777777 ,
Quote a reply from an old similar thread SQL Server Bulk Insert Update from CSV
Not directly, no. You need to BULK INSERT into a staging table and then UPDATE the existing records and INSERT the missing records. I would try with a local temp table (i.e. #TableName) first.
Technically speaking, you could probably do either of the following (both of which use OPENROWSET):
• Skip the staging table and use OPENROWSET(BULK...) for the UPDATE and then INSERT queries. This would have to be tested, though, to see if the cost of reading the file twice is worth the savings of not having to read it into a temp table (which just writes it back to disk). It is possible that using the staging table might still be better since the first query, the UPDATE, might auto-create some statistics that would benefit the second query, the INSERT, since that query will need either a LEFT JOIN or a WHERE NOT EXISTS.
• MERGE along with OPENROWSET(BULK...), but MERGE has some "issues" so I wouldn't try this.
Hope this could help you.
If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".