--select iFabricOrder, iPackageOrder,tPackageTime into #t1 from dbo.mmInDtl where sOrderNo = 'MLS_J210409M' and tPackageTime is not null declare @i int = 0, @t datetime --一定要在更新语句中初始化变量,再给字段赋值 update A set @i = case @t when tPackageTime then @i else @i+1 end, iPackageOrder = @i, @t = tPackageTime from (select top 10000 * from dbo.mmInDtl where sOrderNo = 'MLS_J210409M' and tPackageTime is not null order by tPackageTime) A --select * from #t1 order by tPackageTime --drop table #t1 select iPackageOrder, tPackageTime from dbo.mmInDtl where sOrderNo = 'MLS_J210409M' and tPackageTime is not null order by tPackageTime