これは、このディスカッションのテスト テーブルです。
create table t4721736 ( id int identity primary key, name varchar(10), comm money )
insert t4721736 select 'John', 232.43 -- id=1
insert t4721736 select 'Alex', 353.52 -- id=2
-- check contents
select * from t4721736
-- do all this in a transaction
BEGIN TRAN
-- dummy insert
insert t4721736 select 'dummy', null
-- get what the id should be
declare @resetto bigint
set @resetto = scope_identity()
-- remove dummy record
delete t4721736 where id = @resetto
-- perform the insert(s)
set identity_insert t4721736 on;
insert t4721736(id,name,comm) select 10000000, 'Smith', 334.23;
set identity_insert t4721736 off;
-- reset the identity
set @resetto = @resetto - 1 -- it needs to be 1 prior
DBCC CHECKIDENT(t4721736, RESEED, @resetto)
COMMIT
範囲が指定された ID を持つレコードに達するとすぐに失敗することを完全に理解していると仮定します (そうだと思います)。SQL Server は、既にレコードが添付されている ID に対して自動スキップを実行しません。
これは問題にはなりません。identity_insert を使用して挿入すると、id の値は 1,000 万を超えます。したがって、衝突の問題はありません
これがどのように失敗するかを確認するには、上記のコードで「10000000」を「10」に変更してプロセスをショートカットします。次に、これらをフォローアップします。
-- inspect contents, shows records 1,2,10
select * from t4721736
-- next, insert 7 more records, bringing the id up to 9
insert t4721736 select 'U3', 0
insert t4721736 select 'U4', 0
insert t4721736 select 'U5', 0
insert t4721736 select 'U6', 0
insert t4721736 select 'U7', 0
insert t4721736 select 'U8', 0
insert t4721736 select 'U9', 0
最後に、以下の次の挿入を試してください
insert t4721736 select 'U10', 0