How to apply Update if an item exists and Insert otherwise

ぐ巨炮叔叔 提交于 2019-12-12 02:09:35

问题


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.



来源:https://stackoverflow.com/questions/7873373/how-to-apply-update-if-an-item-exists-and-insert-otherwise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!