CTE Row_Number().
@YourTable
- .
, over (Order by Item)
. over (Order by (Select NULL))
.
Declare @YourTable Table (OrderNumber varchar(50),[Item] varchar(50),[LineNumber] int,[xzy] varchar(50))
Insert Into @YourTable Values
(2525,134,10000,'blah blah')
,(2525,1432,20000,'blah blah blah')
,(2525,433,NULL,'blah blah')
,(2525,839,NULL,'blah blah')
,(5050,500,70000,'blah blah')
,(5050,600,80000,'blah blah blah')
,(5050,700,NULL,'blah blah')
;with cte as (
Select *
,RN =10000 * Row_Number() over (Partition By OrderNumber Order by Item) + (Select max(LineNumber) from @YourTable Where OrderNumber=A.OrderNumber)
From @YourTable A
Where LineNumber is null
)
Update cte Set LineNumber = RN
Select * from @YourTable
OrderNumber Item LineNumber xzy
2525 134 10000 blah blah
2525 1432 20000 blah blah blah
2525 433 30000 blah blah
2525 839 40000 blah blah
5050 500 70000 blah blah --<< Notice New OrderNumber
5050 600 80000 blah blah blah
5050 700 90000 blah blah