我想在創建日期中使用row_number函數。如果您正在批量插入,那麼您還需要按row_number函數中的id列進行排序。
declare @records table (id int identity(1,1), CreationDate datetime, Name varchar(50), Section char(1), FileID varchar(10))
insert into @records (CreationDate, Name, Section)
select '2011-01-26 16:57:49', 'abc','p'
insert into @records (CreationDate, Name, Section)
select '2011-01-26 16:57:50', 'def','p'
insert into @records (CreationDate, Name, Section)
select '2011-01-26 16:58:00', 'ghi','c'
insert into @records (CreationDate, Name, Section)
select '2011-01-26 16:58:50', 'jkl','d'
insert into @records (CreationDate, Name, Section)
select '2011-01-26 16:58:51', 'mno','c'
insert into @records (CreationDate, Name, Section)
select '2011-01-26 16:58:52', 'pqr','p'
insert into @records (CreationDate, Name, Section)
select '2011-01-26 16:58:53', 'def','p'
update @records
set FileID=a.FileID
from
(
select id,
Section + cast(row_number() over (partition by Section order by CreationDate, Section) as varchar(10)) as FileID
from @records
) a
inner join @records b
on a.id=b.id
select * from @records
'sequential'方面有多重要?插入失敗會留下任何空隙嗎? –
不幸的是非常重要 - 因爲當文件從存儲中丟失時(在歸檔之後它們一直被取出),它可以更容易地進行身份識別 - 顯然,如果它們是數字中的空白,那麼您不會知道如果文件丟失或未分配號碼 –
確定完全排除「身份」,那麼根本不能保證這一點(即使您有3個不同的表,您仍然可以從回滾事務中獲得差距) –