Update a given table field - SSMS V18

Asked

Viewed 23 times

-2

I’m filling a table with data that I pick up in a JSON, as shown in the image:

When the markettype is 'place', does not have the correct information of 'Category' and 'Distance'. When the markettype is 'win', have the correct information. I wanted to fill out this two information with the correct data, and tried to do some update comparing 'marketdate' and 'idrunn', to make sure it’s the same event, and marketype = 'win', take the two values I need and update on the line 'place', but it didn’t work... The situation is too complex for my little knowledge.

In short, it would be to catch the marketdate and the idrunn of the line marketype = 'win' and compare with lines markettype = 'place'. When the marketdate and the idrunn for equals, means it’s the same event there would be update the field 'Category' and 'Distance' of the line 'place'.

Could someone help me?

  • and what have you tried to do? put the query to the question. But you will need to do a subquery to get the other value

  • See if it helps (update with self-join) https://stackoverflow.com/questions/2380353/update-with-self-join

1 answer

0

In case an easy way for you to do this is by creating a cursor

kind of:

declare seuCursor cursor
    local for 
    --select atr1,atr2... from suatabela

declare 
    --@variaveis que for utilizar
    @atr1 varchar(200),
    @atr2 varchar(200);

open seuCursor
fetch next from seuCursor into @atr1,@atr2... --as variaveis, 
--lembrando que tem que ser de acordo com o select. se seu select é select nome, idade from suatabela seu into será into @nome, @idade
while @@FETCH_STATUS = 0
begin
    --cria a sua logica
    --if(@variavel for igual a tal)
    --begin
        --update alguma coisa
    --end

    -- repete o fetch next from into
end
close seuCursor

DEALLOCATE seuCursor```

Browser other questions tagged

You are not signed in. Login or sign up in order to post.