1. 程式人生 > >sqlserver 遊標的使用

sqlserver 遊標的使用

create table demok(id int ,name varchar(20));
insert into demok values(1,'a');
insert into demok values(2,'b');
go
begin
 declare @id int,@name varchar(20) 
 declare cur_order cursor for  --申明遊標
  select id,name from demok
 
  open cur_order --開啟遊標
  fetch next from   cur_order into @id,@name
  
  while
@@FETCH_STATUS=0 begin print @name+ convert(varchar(20),@id) fetch next from cur_order into @id,@name end close cur_order --關閉遊標 DEALLOCATE cur_order--釋放遊標 end