Updating database records in a loop?

前端 未结 6 792
轮回少年
轮回少年 2021-02-10 09:19
declare
begin
  for i in (select * from emp)
  loop
    if i.sal=1300 then
      update emp
      set sal=13000;
    end if;
  end loop;
end;

This code

6条回答
  •  误落风尘
    2021-02-10 09:50

    While some of these solutions are workable its not a one size fits all solution. I ran into a scenario where we had to set an xml/nvarchar(max) field to null on a table that has +50 M records. This is an excerpt of the code

    begin
    
    declare @rows int,
            @y    int = 2020,
            @m    int = 1;
    
    set @rows = 1;
    
    while (@rows > 0)
    begin
    
    update top (500) cr
       set [xml] = null
     from [dbo].[customer] cr with(index(ix_customer_reportdt)) 
     where  year([reportdt]) = @y
       and month([reportdt]) = @m
       and [xml] is not null;
    
      set @rows = @@rowcount;
    
    end
    
    end
    

提交回复
热议问题