SQL Serverでなく、MySQLです、済みません。テーブル名はtと仮定しました。

create procedure setB()
begin
 declare done int;
 declare _flag int;
 declare _cnt int;
 declare _a int;
 declare _no int;

 declare cur cursor for select No,A from t order by No desc;
 declare exit handler for not found set done = 0;
 set done = 1;
 set _flag = 0;
 set _cnt = 0;
 
 open cur;
 while done do
  fetch cur into _no, _a;
  if _a = 1 and _no > 0 then
   update t set b = 0 where No = _no + 1;
   set _cnt = _cnt + 1;
  else
   update t set b = _cnt where No = _no + 1;
   set _cnt = 0;
  end if;
 end while;

 if _a = 1 then
  update t set b = 0 where No = _no;
 else
  update t set b = _cnt where No = _no;
 end if;
 close cur;
end;