I have a table
| code | descd | slnum |
|------|-------|-------|
| 10 | a | 0 |
| 10 | b | 0 |
| 12 | c | 0 |
| 12 | d | 0 |
| 11 | e | 0 |
| 11 | f | 0 |
And I have to update slnum column like this using cursor having loops
| code | descd | slnum |
|------|-------|-------|
| 10 | a | 1 |
| 10 | b | 2 |
| 12 | c | 1 |
| 12 | d | 2 |
| 11 | e | 1 |
| 12 | f | 3 |
How to resolve this? I have tried like this but its not giving me correct output
DECLARE @value INT
DECLARE @s INT=1
DECLARE scursor CURSOR FOR
SELECT slnum
FROM trec
for update of slnum
OPEN scursor
FETCH NEXT FROM scursor
INTO @value
WHILE @@FETCH_STATUS = 0
BEGIN
if exists(select * from trec) -- missing
begin
update trec
set slnum=@s
where current of scursor
select @s=@s+1
end
else
begin
update trec
set slnum=@s
where current of scursor
end
FETCH NEXT FROM scursor INTO @value
END
CLOSE scursor
DEALLOCATE scursor
ORDER BY code, descdclause