Search code examples
sqlsql-serversql-server-2014

generate sequence in sql server 2014


I am trying to generate a sequence varchar text type, but I do not want to have to create another column to get the id to format it and insert it I want to generate it in the same column, help

create table tbl (
  Id int identity not null,
  CusId as 'CUS' + format(Id, '00000'),
  -- ...
)

Solution

  • Believe the only viable solution is using 2 columns as you mentioned, and discussed here:
    Autoincrement of primary key column with varchar datatype in it

    Have not seen it achieved in a single column on its own.