How to apply Update if an item exists and Insert

2019-08-20 16:36发布

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

enter image description here

Here is a samples of data:

enter image description here

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

enter image description here

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

enter image description here

1条回答
Emotional °昔
2楼-- · 2019-08-20 17:14

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.

查看更多
登录 后发表回答