虽然很多大牛建议不要用游标,但是实际情况又不能不用地。

一般在mssql中经常用的会有while和游标cursor。

--for循环一般是在知道数据范围内
select 0 as aint into mytable
declare @i int
set @i=0
while @i<=100
begin
     insert into mytable select @i
     set @i=@i+1
end  
select sum(aint) from mytable

 

--游标常用于重数据表中取出参数

declare @cur char(20)  --定义接收游标参数

declare cur cursor  for select [distinct] name from goods [where] order by name    --定义游标,

open cur  -1 打开游标
     fetch  next from cur into @cur --取出第一条记录,将值出入@cur,初始化
             insert into newtable
              select * from order where name=@cur    
    fetch next cur into @cur       --移到下一条记录,执行上面的语句继续循环,相当于游标是个数组
close cur  --关闭游标
deallocate cur  --处理游标,释放

相关文章:

  • 2021-10-28
  • 2022-12-23
  • 2021-07-27
  • 2021-05-22
  • 2022-12-23
  • 2021-12-29
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-11-05
  • 2022-12-23
  • 2022-12-23
  • 2021-12-28
  • 2022-12-23
  • 2022-12-23
  • 2021-09-06
相关资源
相似解决方案