Skip to content
Advertisement

How to update row if exist and meet more than one condition and insert it otherwise in SQL Server

I’m trying to perform the below code which I have found it in a thread here

The problem is that I need to do the same thing but with the SQL Server query. My main thought here is that if the new read has the same BoxNo and the same SSCC and the same StyleBarcode, then the value in Qty column will be updated by +1. Otherwise, it’ll make a new insert.

I’m thinking to make something like the following code:

But I couldn’t make it because I don’t have something like the ROW_COUNT() in SQL Server.

Or I don’t know if you can make have it done in another way?

Thanks in advance.

My table:

MyTestTabel

Advertisement

Answer

SQL Server implements the merge syntax, which seems relevant for your use case.

This statement lets you insert or update from one table into a given table , according to well-defined predicates.

For your question, that would be:

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement