How to generate Autogenerated varchar Sequence in SQL Server?
Step :1 Create the Table
CREATE TABLE dbo.tbl_sample ( [ID] VARCHAR(8) , [Name] varchar(50)
CONSTRAINT PK_Employee_EmployeeID PRIMARY KEY CLUSTERED ([ID] ASC) )
Step :2 Create the SEQUENCE
CREATE SEQUENCE dbo.Sample_Seq AS INT START WITH 1 INCREMENT BY 1 ;
Step :3 Alter the Employee Table and addthe default Constraint
ALTER TABLE dbo.tbl_sample ADD CONSTRAINT Const_Sample_Seq
DEFAULT FORMAT((NEXT VALUE FOR dbo.Sample_Seq),'CUS_#') FOR [ID];
Step :4 Insert the Employee Data
INSERT INTO dbo.tbl_sample ([Name]) VALUES ('Imran'),('Bob'),('Sandra')
Step :4 Show the Data
Select * from dbo.tbl_sample
No comments:
Post a Comment