Sequences

Create Sequence

CREATE SEQUENCE [dbo].[CustomersSeq]
AS INT
START WITH 10001
INCREMENT BY 1
MINVALUE -1;

Delete From & Insert New

DELETE FROM [dbo].[Customers]
WHERE CustomerName = 'Gorge';

INSERT INTO [dbo].[Customers]
       ([CustomerName])
 VALUES ('George')

SELECT * FROM [dbo].[Customers]

Results

CustomerIDCustomerName
10001Jerry
10003George

Insert Into Table with Sequence

INSERT INTO [dbo].[Customers]
       ([CustomerName])
 VALUES
       ('Jerry'),
       ('Gorge')

SELECT * FROM [dbo].[Customers]

Results

CustomerIDCustomerName
10001Jerry
10002Gorge

Use Sequence in Table

CREATE TABLE [dbo].[Customers]
(
    CustomerID INT DEFAULT (NEXT VALUE FOR [dbo].[CustomersSeq]) NOT NULL,
    CustomerName VARCHAR(100),
);


2016-08-09
2016-08-09
Microsoft SQL Server Pedia
Icon