I want to make this column ID that is a varchar field be generated automatically, when I insert the name, as if it were a sequence, help
A sequence can only generate numerical values, not values with formatted characters as you search (example: CUS00001
). So, even if you could use a sequence, it would be up to you to use the value it returns and apply the desired format.
As discussed under your question, a way that would seem convenient to what you want to do is through a calculated column. But sadly, SQL Server does not seem to allow the use of sequences in the definition of a calculated column.
Probably, the simplest option in your case is, instead of using a sequence, that bases the column calculated in another column IDENTITY
:
create table tbl (
Id int identity not null,
CusId as 'CUS' + format(Id, '00000'),
-- ...
)
Of course, this means adding an additional column IDENTITY
to keep the numerical value. Otherwise, you would have to explore a solution using a trigger to update the column using a sequence, but this would have other disadvantages.