declare @id int declare cur cursor for select id from t where zid=0 open cur fetch next from cur into @id while @@Fetch_status = 0 begin update t set replies=(select count(*) from t where zid =@id) where zid=0 and id=@id fetch next from cur into @id end select * from t close cur deallocate cur
------解决方案-------------------- update tb set replies=(select isnull(count(*),0) from Tb a where a.id=id) where zid=0