SQL Server游标与循环示例

【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	

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

declare @i int 
set @i=1 
while @i<30 
   begin 
   insert into test (userid) values(@i) 
   set @i=@i+1 
end

你可能感兴趣的:(开发日常问题,sql,数据库,database)