It is true what has been said about the fact that you can not do an auto-incremental with text. But there is a trick that could help you.
Define a column INT IDENTITY
in your table
Add a calculated column that is the combination between a string and the ID
:
Something like this:
CREATE TABLE dbo.TuTabla
(ID INT IDENTITY(1,1),
COD_RES AS 'RES_' + CAST(ID AS VARCHAR(10)) PERSISTED,
......
)
This way, your column ID
is a IDENTITY
normal, and COD_RES
will have values like RES_1, RES_2, RES_3, ......, RES_50
With the statement persisted
the column is saved on the disk, and it is not necessary to calculate it every time the table is consulted.
Note : If you use my suggestion, your insert would be like this:
INSERT INTO RESPONSABLE VALUES('Pedro', 'Abanto', 'Mendoza')
You would not specify the value of the Id (which is autoincremental) nor the calculated field, because it will be automatically generated from the text you have predefined along with the Identity value.