SQL Server identity counterpart problem

泄露秘密 提交于 2020-01-06 12:56:34

问题


I'm using SQL Server and I want to use identity constraint in it

I know how to use it in following manner

create table mytable
(
 c1 int primary key identity(1,1);
)

the above code works fine but what if i want the identity column to have values as EMP001, EMP002,... instead of 1,2....

Thanks in advance, Guru


回答1:


Identity columns can only be integers. If you want it to "look like" EMP001, EMP002, etc then that's simply a display issue and not a storage one (that is, you don't need to store "EMP001", "EMP002" etc, just store it as 1, 2, 3 via a normal identity column and display it in your application as "EMP001", "EMP002", etc)




回答2:


Create a computed column that concatenates like this:

'EMP' + RIGHT('00' + CAST(c1 AS varchar(3)), 3)

Otherwise an IDENTITY column as surrogate key is just that: a meaningless number.

I assume you're only going to have 999 rows or is there another sequence somewhere?



来源:https://stackoverflow.com/questions/2486483/sql-server-identity-counterpart-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!