This article is half-done without your Comment! *** Please share your thoughts via Comment ***
SQL Server 2012 introduced a Sequence object with lots of functionality, but unfortunately, we can only create Sequence Object with Number data-types.
We can create a Sequence object for a list of below data-types:
tinyint
smallint
int
bigint
decimal
numeric
But in some situations, we also require alphanumeric Sequence Number, and for that, we should apply special FORMAT on the value of Sequence Number.
Below is a full demonstration to FORMAT a Sequence value and make it alphanumeric.
First, Create sample table:
1 2 3 4 5 6 7 |
CREATE TABLE dbo.tbl_TestVarcharSequence ( VarcharID VARCHAR(10) ,Name VARCHAR(255) ,CONSTRAINT pk_tbl_TestVarcharSequence_VarcharID PRIMARY KEY(VarcharID) ) GO |
Create a Sequence object:
1 2 3 4 |
CREATE SEQUENCE dbo.seq_TestVarcharSequenceNumber AS INT START WITH 1 INCREMENT BY 1; GO |
Create Sequence constraint using FORMAT function:
1 2 3 4 5 |
ALTER TABLE dbo.tbl_TestVarcharSequence ADD CONSTRAINT seq_tbl_TestVarcharSequence_VarcharID DEFAULT FORMAT((NEXT VALUE FOR dbo.seq_TestVarcharSequenceNumber),'ABC000000#') FOR VarcharID; GO |
Insert few sample records:
1 2 3 |
INSERT INTO dbo.tbl_TestVarcharSequence (Name) VALUES ('Anvesh'),('Alex'),('Roy'),('Bony') GO |
The Result:
1 |
SELECT * FROM dbo.tbl_TestVarcharSequence |
You can find Alphanumeric Sequence Number in the above result.