IDENTITY(1, 1) は、メモリ最適化テーブルでサポートされています。 ただし、IDENTITY(x, y) の定義を持つ ID 列 (x != 1 または y != 1) は、メモリ最適化テーブルではサポートされていません。 IDENTITY 値の回避策では、SEQUENCE オブジェクト (シーケンス番号) を使用します。
最初に、OLTP に変換するテーブルから IDENTITY プロパティ In-Memory 削除します。 次に、テーブル内の列の新しい SEQUENCE オブジェクトを定義します。 ID 列としての SEQUENCE オブジェクトは、NEXT VALUE FOR 構文を使用して新しい ID 値を取得する列の DEFAULT 値を作成する機能に依存します。 DEFAULT は In-Memory OLTP ではサポートされていないため、新しく生成された SEQUENCE 値を INSERT ステートメントまたは挿入を実行するネイティブ コンパイル ストアド プロシージャに渡す必要があります。 次の例は、このパターンを示しています。
-- Create a new In-Memory OLTP table to simulate IDENTITY insert
-- Here the column C1 was the identity column in the original table
--
create table T1
(
[c1] integer not null primary key T1_c1 nonclustered,
[c2] varchar(32) not null,
[c3] datetime not null
) with (memory_optimized = on)
go
-- This is a sequence provider that will give us values for column [c1]
--
create sequence usq_SequenceForT1 as integer start with 2 increment by 1
go
-- insert a sample row using the sequence
-- note that a new value needs to be retrieved form
-- the sequence object for every insert
--
declare @c1 integer = next value for [dbo].[usq_SequenceForT1]
insert into T1 values (@c1, 'test', getdate())
挿入を複数回実行すると、列 [c1] に有効な単調に増加する値が表示されます。 この結果セットは、テーブル スキャンとハッシュ インデックスを使用して ORDER BY
を使用せずに生成されるため、行は順序付けされません。