on-internet
← Community

Auto-increment ID with Prefix

Stack Overflow ·

I want to achieve an auto increment ID with prefix but resetting the number if it has different prefix. The output I want looks like this: ID PREFIX PROJECTID 1 PID_ PID_1 2 PID_ PID_2 3 RID_ RID_1 4 RID_ RID_2 But the result I got with my script is this: ID PREFIX PROJECTID 1 PID_ PID_1 2 PID_ PID_2 3 RID_ RID_3 4 RID_ RID_4 Here's my script to create the table CREATE TABLE PROJECTS (ID INT IDENTITY(1,1) NOT NULL, PREFIX NVARCHAR(10) NOT NULL, PROJECTID AS ISNULL(PREFIX + CAST(ID AS NVARCHAR(10)), '') PERSISTED) INSERT INTO PROJECTS(PREFIX) VALUES('PID_'),('PID_'),('RID_'),('RID_') I'm using

Comments

Sign in or sign up to comment and upvote.

No comments yet.