Skip to content
Advertisement

How to add if/else condition in SQL Server query?

I have to parse a json like this:

I have to parse it and check if the name already exists in userInfo table. If the name is found in that table, then I only update the age.

Else, I have to insert the data into the userInfo table.

Here’s what I did so far:

Now I want to insert or update these data in table userInfo.

If the name from #tmp table already exists in userInfo table, then update the age, else insert the data into the userInfo table.

I don’t know what the syntax for this operation is, or if it is possible to do.

Advertisement

Answer

You can use MERGE keyword for this

Just to be more succinct I’d do the same this way

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