declare @CryoboxID int = 1;
; with available as (select
row_number() over (order by tally.number) n, tally.number Cryoslot
from master.dbo.spt_values tally left join dbo.SampleTable s
on tally.number = s.Cryoslot and CryoboxID = @CryoboxID
where
tally.number between 1 and 100 and tally.[type] = 'P'
and s.ID_Text is null /* get avaiable numbers only */
),
samples as (
select top 100
row_number() over (order by ID_Text) n, CryoboxID, Cryoslot
from dbo.SampleTable
where CryoboxID is null and Cryoslot is null
)
update s set CryoboxID = @CryoboxID, Cryoslot = a.Cryoslot
from samples s inner join available a
on s.n = a.n;
Please let me know is this helps.
18 People are following this question.