use ddf_jg declare @id int; declare @order nvarchar(50); declare @count int; declare @maxcount int; declare @OKCount int; declare @status nvarchar(10); with subtbl as ( select id, count(*) as cot from dindansp where spid is not null and spid <> '' group by id )select id as [order],cot into #tempall from subtbl --3.��ȡָ���ֶεIJ��ظ���¼,���˵�����ȫ�ظ��ļ�¼; select identity(int,1,1) as id, * into #tempresult from #tempall select @maxcount = count(id) from #tempresult while @maxcount > 0 begin -- ��ȡָ�������м�¼; select @order = [order], @count = cot from #tempresult where id = @maxcount -- ��ȡ select @OKCount = count(status2) from dindansp where id = @order and status2 = 'OK' and spid is not null and spid <> '' -- ��ȡ����ȡ��״̬; select @status = status3 from dindan where id = @order if (@status <> 'OK' and @count = @OKCount) begin update dindan set status3 = 'OK' where id = @order print '���¶���' + @order print @count print @maxcount print @OKCount end set @maxcount = @maxcount -1 end drop table #tempall drop table #tempresult