から毎日のシーケンス番号を取得するための良い方法は、それに巻き付け非常に制限トランザクションなしで起こるのを待っている事故のように見えるがありますです。
EDIT:Row_Number()
の計算は、すべての行の番号付けに依存することを強調しました。
いつもINSERT
を実行し、結果を使って表示された毎日のIdを生成するのはどうですか?あなたはこれに有用な何かを見ますか:
-- Create some sample data.
declare @Today as Date = GetDate()
declare @Stuff as Table (Id Int Identity, DateAdded Date)
insert into @Stuff (DateAdded) values
(DateAdd(day, -3, @Today)),
(DateAdd(day, -2, @Today)), (DateAdd(day, -2, @Today)),
(DateAdd(day, -1, @Today)), (DateAdd(day, -1, @Today)),
(@Today), (@Today)
-- Display the test data.
select Id, DateAdded,
Id - (select Min(Id) from @Stuff where DateAdded = S.DateAdded) as DailyId,
Row_Number() over (partition by DateAdded order by Id) as AlternativeDailyId,
Convert(VarChar(8), DateAdded, 112) + '-' +
Right('000000' + Cast(Id - (select Min(Id) from @Stuff where DateAdded = S.DateAdded) as VarChar(10)), 6) as DateId
from @Stuff as S
-- Add a row.
declare @NewRows as Table (Id Int, DateAdded Date)
insert into @Stuff
(DateAdded)
output inserted.Id, inserted.DateAdded into @NewRows
values (GetDate())
-- Display the new Id.
-- Note the the Row_Number() calculation only works when all rows are counted.
select N.Id, N.DateAdded,
N.Id - (select Min(Id) from @Stuff where DateAdded = N.DateAdded) as DailyId, -- NB: References @Stuff.
Row_Number() over (partition by N.DateAdded order by N.Id) as AlternativeDailyId,
Convert(VarChar(8), N.DateAdded, 112) + '-' +
Right('000000' + Cast(Id - (select Min(Id) from @Stuff where DateAdded = N.DateAdded) as VarChar(10)), 6) as DateId
from @NewRows as N
select *
from (
select S.Id, S.DateAdded,
S.Id - (select Min(Id) from @Stuff where DateAdded = S.DateAdded) as DailyId, -- NB: References @Stuff.
Row_Number() over (partition by S.DateAdded order by S.Id) as AlternativeDailyId,
Convert(VarChar(8), S.DateAdded, 112) + '-' +
Right('000000' + Cast(Id - (select Min(Id) from @Stuff where DateAdded = S.DateAdded) as VarChar(10)), 6) as DateId
from @Stuff as S
) as X
where Id in (select Id from @NewRows)
行が削除されると、ひどいことが起こることに注意してください。毎日のIDはスリリングな方法で再計算されます。