17 May 2010

Cursore con “where current of”

set nocount on

-- CREO TABELLA TEMPORANEA ----------------------------------------------------
drop table #p

create table #p
(
 id int
)

insert #p values (10)
insert #p values (20)
insert #p values (30)
insert #p values (40)
insert #p values (50)
insert #p values (60)
insert #p values (70)
insert #p values (80)


select * from #p


-- DICHIARO IL CURSORE --------------------------------------------------------
declare @i int

declare #c cursor local for select id from #p

open #c

fetch next from #c into @i

while @@fetch_status = 0
 begin
  set @i = @i + 100
 
  update #p
  set id = @i
  where current of #c
 
  fetch next from #c into @i
 end


close #c
deallocate #c


select * from #p