sql server - updating a column using cursor having loops -
i have table
| code | descd | slnum | |------|-------|-------| | 10 | | 0 | | 10 | b | 0 | | 12 | c | 0 | | 12 | d | 0 | | 11 | e | 0 | | 11 | f | 0 |
and have update slnum column using cursor having loops
| code | descd | slnum | |------|-------|-------| | 10 | | 1 | | 10 | b | 2 | | 12 | c | 1 | | 12 | d | 2 | | 11 | e | 1 | | 12 | f | 3 |
how resolve this? have tried not giving me correct output
declare @value int declare @s int=1 declare scursor cursor select slnum trec update of slnum open scursor fetch next scursor @value while @@fetch_status = 0 begin if exists(select * trec) -- missing begin update trec set slnum=@s current of scursor select @s=@s+1 end else begin update trec set slnum=@s current of scursor end fetch next scursor @value end close scursor deallocate scursor
i don't know whether or not must use cursor, query dead ringer update
join, using row_number
:
update t1 set slnum = t2.slnum yourtable t1 inner join ( select code, descd, row_number() over(partition code order descd) slnum yourtable ) t2 on t1.code = t2.code , t1.descd = t2.descd
Comments
Post a Comment