記憶體優化數據表支援 IDENTITY(1, 1)。 不過,記憶體優化數據表不支援具有 IDENTITY(x, y) 定義的識別數據行,其中 x != 1 或 y != 1。 IDENTITY 值的因應措施會使用 SEQUENCE 物件 (序號)。
首先,請從您要轉換成 In-Memory OLTP 的數據表中移除 IDENTITY 屬性。 然後,為數據表中的數據行定義新的 SEQUENCE 物件。 作為識別數據行的 SEQUENCE 物件依賴為使用 NEXT VALUE FOR 語法來取得新識別值的數據行建立 DEFAULT 值的能力。 由於 OLTP In-Memory 不支援 DEFAULT,因此您必須將新產生的 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 來產生,因此不會排序數據列。