22,300
社区成员




--> 测试数据:#
if object_id('tempdb.dbo.#') is not null drop table #
create table #(单号 varchar(8), 序号 int, 物料代码 varchar(8), 数量 int)
insert into #
select 'P0001', 1, 'TT.0001', 100 union all
select 'P0001', 2, 'TT.002', 1003 union all
select 'P0001', 3, 'TT.0003', 678 union all
select 'P0001', null, 'TT.004', 666 union all
select 'P0001', null, 'TT.005', 777 union all
select 'P002', null, 'TT.001', 555 union all
select 'P002', null, 'TT.002', 777 union all
select 'P003', 1, 'TT.001', 666 union all
select 'P003', null, 'TT.002', 99 union all
select 'P003', null, 'TT.002', 888
;with cte as
(
select n = row_number()over(partition by 单号 order by getdate()), * from #
)
update cte set 序号=n
select * from #
/*
单号 序号 物料代码 数量
-------- ----------- -------- -----------
P0001 1 TT.0001 100
P0001 2 TT.002 1003
P0001 3 TT.0003 678
P0001 4 TT.004 666
P0001 5 TT.005 777
P002 1 TT.001 555
P002 2 TT.002 777
P003 1 TT.001 666
P003 2 TT.002 99
P003 3 TT.002 888
*/