How do I create a table with a column that uses a sequence?
Assign it as the default property for the column
CREATE TABLE [MyTable]
(
[ID] [bigint] PRIMARY KEY NOT NULL DEFAULT (NEXT VALUE FOR dbo.MyTableID),
[Title] [nvarchar](64) NOT NULL
);
Future readers, a Sequence can have a gap if the service stops unexpectedly, the value of the entities in CACHE can be lost. Here, they are specifying no cache
to mitigate that with the tradeoff of slower performance for the sequence object.
CREATE SEQUENCE reference
To use a SEQUENCE
in an INSERT
statement, you could try this:
INSERT INTO [MyTable] ([ID],[TITLE]) VALUES (NEXT VALUE FOR dbo.MyTableID, @TITLE)
NEXT VALUE FOR dbo.MyTableID
is the syntax for obtaining the next number from a SEQUENCE
.