当前位置:   article > 正文

SQL Server游标与循环示例_sqlserver游标使用和循环

sqlserver游标使用和循环

【1】 游标

declare @ID varchar(20); --ID
declare @filepath varchar(200);--路径



    declare @cursorTmpHelp cursor;--游标
    set @cursorTmpHelp=cursor for 
    select id,filepath from b_project_file  WHERE filepath like '%10.1.1.3%';	
    open @cursorTmpHelp
    fetch next from @cursorTmpHelp into @ID,@filepath;
    WHILE @@FETCH_STATUS=0
    begin
       select @filepath =  filepath from b_project_file where id=@ID
       set @filepath=replace(@filepath,'10.1.1.3','10.1.1.10')
       
       update b_project_file set 
       filepath=@filepath
       where id = @ID
       
       fetch next from @cursorTmpHelp into  @ID,@filepath;
    end
    close @cursorTmpHelp
    deallocate @cursorTmpHelp	
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

【2】循环
while语句使用示例

declare @i int 
set @i=1 
while @i<30 
   begin 
   insert into test (userid) values(@i) 
   set @i=@i+1 
end
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/534229
推荐阅读
相关标签
  

闽ICP备14008679号