0

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

revolutionkpi
  • 2,632
  • 10
  • 45
  • 84

1 Answers1

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.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
  • @ Nicola Musatti, so for example for Property column it should be: create procedure INSERT_OR_UPDATE as begin if exists ( select * from Numerations where Property=null ) begin update Numerations set Property where Property=Property end else begin insert into Numerations values @Property end end – revolutionkpi Oct 24 '11 at 14:21
  • The stored procedure in my example only executes one statement, either an insert or an update. If you need different statements for each row in your table you probably need to write a stored procedure that uses a cursor. – Nicola Musatti Oct 24 '11 at 14:27
  • so, do you know how to write and can you help me with a stored procedure that uses a cursor? – revolutionkpi Oct 24 '11 at 14:31
  • or it can be realised with 3 different stored procedure? – revolutionkpi Oct 24 '11 at 14:37