How to apply Update if an item exists and Insert

2019-08-20 16:21发布

问题:

How to create a procedure that goes from the top of the table and compares the value to null - If a match is found, insert an element in this position. - If not, the element is inserted into a new row

I need to correct a second row which contains null values in 4 last columns regardless of values in the Id and PropertyId columns

Here is a screenshot of my DB

Here is a samples of data:

Now it works so, which is not suitable to me, instead it should update the row with null values like on the last screenshot

But the next entry should overwrite the value of NULL for Item, ItemId, InstanceId and Instance

回答1:

Write a stored procedure like:

create procedure INSERT_OR_UPDATE as
begin
  if exists ( select * from Numerations where <your condition> )
    begin
      update Numerations set < ... > where < ... >
    end
  else
    begin
      insert into Numerations values <...>
    end
end

You have to check the syntax because I cannot test my code right now.