This website completely moved to new platform. For latest content, visit www.programmingposts.com

Search this Site

10 Nov 2012

HOW TO SET IDENTITY KEY/AUTO INCREMENT ON VARCHAR COLUMN IN SQL SERVER

Create the table as follows


CREATE TABLE Student_Register(
SNo INT NOT NULL IDENTITY(1000, 1),
Student_Id AS 'STU_'+CAST( SNo as varchar(10)) PERSISTED PRIMARY KEY,
First_Name VARCHAR(20),
Last_Name VARCHAR(20),
Date_Of_Birth DATE,
Address VARCHAR(100),
Mobile_No BIGINT
)

Here while inserting there is no need to pass values of Sno,Student_Id column.
Then insert the fields like as shown below.



INSERT INTO Student_Register VALUES('sameer','shaik','1990/10/19','hyderabad',9999999999)
INSERT INTO Student_Register VALUES('shafi','shaik','1991/05/27','india',9898989898)

Now displaying the inserted records

SELECT * FROM Student_Register

you can see the inserted records below

No comments:

Post a Comment

Thanks for your comments.
-Sameer